How can i return the output in an array to be used in a following function?

1 Ansicht (letzte 30 Tage)
Hello everyone!,
I am using the following formula to determine the atmospheric absorption based on each frequency according to a given distance. The array of frequencies was given based on a third-octave band.
myFreqArray = [25 32 40 50 63 79 100 126 158 200 251 316 398 501 631 794 1000 ...
1259 1585 1995 2512 3162 3981 5012 6310 7943 10000 12589 15849 19953];
%----------------
for f=myFreqArray
z = 0.1068 * exp (-3352 / T) * (frN + f^2 / frN)^(-1);
y = (T / To)^(-5/2) * (0.01275 * exp (-2239.1 / T) * (frO + f^2 / frO)^(-1) + z);
alpha = 8.686 * f^2 * ((1.84 * 10^(-11) * (pa / pr)^(-1) * (T / To)^(1/2)) + y); %[dB/m]
pt = pi * exp(-x * alpha * d)
AtmosphericAbs=alpha*d;
Aabs=20 * log10(pt/p0);
Gain = Aabs - LdBinitial;
end
%myGainArray = [G1, G2, G3, G4, G5, G6, G7, G8, G9, G10, G11, G12, G13, G14, G15,...
% G16, G17, G18, G19, G20, G21, G22, G23, G24, G25, G26, G27, G28, G29, G30];
release(graphicEQ); % Release system memory from previous run
graphicEQ = oneThirdOctaveGraphicEQ;
oneThirdOctaveGraphicEQ.Bandwidth = '1/3 octave';
oneThirdOctaveGraphicEQ.Gains = myGainArray;
equalizer = graphicEQ;
But I need help please to be able to take the result of the "Gain" output for all the frequencies and arrange them in an array based on the frequency array input. So I can use that output array to generate my graphic Eq.
Thank you for your help!
  2 Kommentare
Torsten
Torsten am 8 Jan. 2023
Which of the other variables you use in your code (T, frN, To, fr0, pa, pr, alpha, d, pt, p0) above are arrays like myFreqArray, which are scalar values ?
charbel hourani
charbel hourani am 25 Jan. 2023
Hello Torsten,
Thank you for your answer, doing the following resolved the problem.
for ii=1:numel(f);

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Sulaymon Eshkabilov
Sulaymon Eshkabilov am 8 Jan. 2023
Here is the corrected code:
myFreqArray = [25 32 40 50 63 79 100 126 158 200 251 316 398 501 631 794 1000 ...
1259 1585 1995 2512 3162 3981 5012 6310 7943 10000 12589 15849 19953];
%----------------
f = myFreqArray; % Renaming is optional that is for the sake of easiness of coding
for ii=1:numel(f);
z = 0.1068 * exp (-3352 / T) * (frN + f(ii)^2 / frN)^(-1);
y = (T / To)^(-5/2) * (0.01275 * exp (-2239.1 / T) * (frO + f(ii)^2 / frO)^(-1) + z);
alpha = 8.686 * f(ii)^2 * ((1.84 * 10^(-11) * (pa / pr)^(-1) * (T / To)^(1/2)) + y); %[dB/m]
pt = pi * exp(-x * alpha * d)
AtmosphericAbs=alpha*d;
Aabs=20 * log10(pt/p0);
Gain(ii) = Aabs - LdBinitial;
end
%myGainArray = [G1, G2, G3, G4, G5, G6, G7, G8, G9, G10, G11, G12, G13, G14, G15,...
% G16, G17, G18, G19, G20, G21, G22, G23, G24, G25, G26, G27, G28, G29, G30];
release(graphicEQ); % Release system memory from previous run
graphicEQ = oneThirdOctaveGraphicEQ;
oneThirdOctaveGraphicEQ.Bandwidth = '1/3 octave';
oneThirdOctaveGraphicEQ.Gains = myGainArray;
equalizer = graphicEQ;
  3 Kommentare
charbel hourani
charbel hourani am 25 Jan. 2023
Dear Sulaymon,
Sorry for reaching out again.
Following the excution of the previous code that you have helped me with.
I am trying to build the code based on the ISO 9613-1 standard, that can filter an audio track based on atmospheric absorption.
In the following code, I created a user interface where I can input, Altitude, temperature, Humidity, Initial level and distance.
But when I started adding the apply button and call back functions to make sure that all the variables are entered. I am encountering problems with the "deviceWriter(audioOut);"
With the following error message :
Not enough input arguments.
Error in LiveEditorEvaluationHelperE678951713>apply_callback (line 133)
if ~isDeviceOpened
Error while evaluating UIControl Callback.
Would it be possible to have your opinion on this code, please, to resolve the issue by executing correctly the part where the audio must be played out on the computer speakers after being filtered?
Thank you in advance
% Create a user interface to input data
f = figure;
% Create text boxes for the different variables
global t1 t2 t3 t4 t5 isDeviceOpened
isDeviceOpened = false;
t1 = uicontrol('Style', 'edit', 'String', '10', 'Position', [20, 350, 100, 20]);
t2 = uicontrol('Style', 'edit', 'String', '20', 'Position', [120, 350, 100, 20]);
t3 = uicontrol('Style', 'edit', 'String', '70', 'Position', [220, 350, 100, 20]);
t4 = uicontrol('Style', 'edit', 'String', '100', 'Position', [320, 350, 100, 20]);
t5 = uicontrol('Style', 'edit', 'String', '', 'Position', [420, 350, 100, 20]);
% Create a button to apply the callback function
apply_button = uicontrol('Style', 'pushbutton', 'String', 'Apply', 'Position', [20, 100, 80, 20], 'Callback', {@apply_callback, t1, t2, t3, t4, t5});
% Create labels for the text boxes
uicontrol('Style', 'text', 'String', 'Altitude [m]:', 'Position', [20, 375, 100, 30]);
uicontrol('Style', 'text', 'String', 'Tempreture [ºC]:', 'Position', [120, 375, 100, 30]);
uicontrol('Style', 'text', 'String', 'Humidity [%]:', 'Position', [220, 375, 100, 30]);
uicontrol('Style', 'text', 'String', 'Initial Sound Level [dB]:', 'Position', [320, 375, 100, 30]);
uicontrol('Style', 'text', 'String', 'Distance [m]:', 'Position', [420, 375, 100, 30]);
oneThirdOctaveGraphicEQ = graphicEQ;
graphicEQ = oneThirdOctaveGraphicEQ;
function apply_callback(hObject, eventdata, t1, t2, t3, t4, t5, isDeviceOpened)
% Get the value of the edit text box
Altitude = str2double(get(t1, 'String')); % Expressed in meters over see level
Tc = str2double(get(t2, 'String')); % Tempretures in Degree Celsius
hr = str2double(get(t3, 'String')); % Relative humidity as a percentage
LdBi = str2double(get(t4, 'String')); % Initial SoundLevel in dB at 1m from source
d = str2double(get(t5, 'String')); % Distance in meters from source
if isnan(Altitude) || isnan(Tc) || isnan(hr) || isnan(LdBi) || isnan(d)
error('Invalid input')
end
%---------------------------------------------------------------------------------------------
%Initial Reference Data
Tk= Tc + 273.15; % Ambient atmospheric tempreture in Kelvin K
To=293.15; % Reference atmospheric tempreture in Kelvin 20C
pr=101.325; % Reference ambient atmospheric pressure in kPa
pa = pr * exp((-9.80665 * 0.0289644 * Altitude)/(8.31432 * Tk));% Ambient atmospheric pressure in kPa based on altitude
To1=273.16; % Triple-point isotherm temp: 273.16 K = 273.15 + 0.01 K (0.01°C)
p0=0.00002;
x=0.1151; % A help factor to shorten the formula
pi=p0*10^(LdBi/20); % Initial sound pressure ampliture in Pa
psat = pr * 10^((-6.8346 * (To1 / Tk)^1.261) + 4.6151); % Saturation vapor pressure
h = hr * (psat / pa); % Molar concentration of water vapor as percentage
frO = (pa / pr) * (24 + 4.04 * 10^4 * h * ((0.02 + h) / (0.391 + h))); % Oxygen relaxation frequency
frN = (pa / pr) * (Tk / To)^(-1/2) * (9 + 280 * h * exp(-4.170 * ((Tk / To)^(-1/3)-1))); % Nitrogen relaxation frequency
%---------------------------------------------------------------------------------------------
% Third Octave center frequncy for the garphic EQ in Hz based on ISO
oneThirdCenterFreqArray = [25 31.5 40 50 63 80 100 125 160 200 250 315 400 500 630 800 1000 ...
1250 1600 2000 2500 3150 4000 5000 6300 8000 10000 12500 16000 20000];
%---------------------------------------------------------------------------------------------
f = oneThirdCenterFreqArray; % Third Octave center frequncy for the garphic EQ in Hz
for ii=1:numel(f)
z = 0.1068 * exp (-3352 / Tk) * (frN + f(ii)^2 / frN)^(-1); % A help factor to shorten the formula
y = (Tk / To)^(-5/2) * (0.01275 * exp (-2239.1 / Tk) * (frO + f(ii)^2 / frO)^(-1) + z); % A help factor to shorten the formula
alpha = 8.686 * f(ii)^2 * ((1.84 * 10^(-11) * (pa / pr)^(-1) * (Tk / To)^(1/2)) + y); % Pure tone attenuation coeffiecnt in dB/m for Atmospheric Absorption
pt = pi * exp(-x * alpha * d); % Sound pressure amplitude in Pa
AtmosphericAbs=alpha*d; % Atmospheric attenuation due to distance
Aabs=20 * log10(pt/p0); % Atmospheric attenuation due to distance in dB
Gain(ii) = Aabs - LdBi; % Atmospheric attenuation due to distance in dB per frequency
end
%---------------------------------------------------------------------------------------------
% Import Audio Track from Directory
frameLength = 1024;
fileReader = dsp.AudioFileReader('/Users/user/Documents/Music repertoire/TEST MUSIC/What Is Love 2016.mp3','SamplesPerFrame',frameLength);
deviceWriter = audioDeviceWriter('SampleRate',fileReader.SampleRate);
% ------------------------------
% Perform Graphic EQ Filtering according to Absorption Calculations
%release(graphicEQ); % Release system memory from previous run
oneThirdOctaveGraphicEQ.Bandwidth = '1/3 octave';
oneThirdOctaveGraphicEQ.Gains = Gain;
equalizer = graphicEQ;
visualize(equalizer);
% Playing the filtered audio out
while ~isDone(fileReader)
audioIn = fileReader();
audioOut = equalizer(audioIn);
%Opening the audioDeviceWriter
if ~isDeviceOpened
deviceWriter = audioDeviceWriter('SampleRate',fs);
setup(deviceWriter);
isDeviceOpened = true;
end
%Playing the sound
deviceWriter(audioOut);
% ...
% ...
% Close the audioDeviceWriter
release(deviceWriter);
drawnow limitrate % required to update parameter
end
end

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Audio Processing Algorithm Design finden Sie in Help Center und File Exchange

Produkte


Version

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by