# mixing audios according to array value

1 view (last 30 days)
Mohamed Turkmani on 1 Sep 2022
Commented: Mathieu NOE on 2 Sep 2022
hi i have the following code which mixes two audios and plots them :
Fs = 44100;
[y,Fs] = audioread("sound and 1000 new.wav");
mix = y+x;
sound(mix,Fs)
Nsamps = length(mix);
t = (1/Fs)*(1:Nsamps)
y_fft = abs(fft(mix));
y_fft = y_fft(1:Nsamps/2);
f = Fs*(0:Nsamps/2-1)/Nsamps;
figure
plot(f, y_fft)
xlim([0 5000])
xlabel('Frequency (Hz)')
ylabel('Amplitude')
the values of y and x are x = 192000 and y = 192000
now i create a simple sine tone by the following code:
Fs = 44100;
dt = 1/Fs;
StopTime = 3
t = (0:dt:StopTime-dt);
Fc = 1000;
A = 1;
z = A*sin(2*pi*Fc*t);
sound(z,Fs)
the value of z is
as we can see Z is different than X and Y although they are all 3 seconds so i cant mix them, how can i generate a simple sine tone with tha value of Z similar to X and Y so im able to mix them?

Mathieu NOE on 1 Sep 2022
hello
the simplest trick here is to first generate z with the same dimension (length) as x and y
use the initial t vector already defined for x / y in the first section of your code
then simply force the values of z for t > StopTime to zero
Fs = 44100; % nb : already defined in first section, no need to duplicate this line
dt = 1/Fs;
StopTime = 3
StopTime = 3
% t = (0:dt:StopTime-dt); not used here , use t = (1/Fs)*(1:Nsamps)
% already defined in first section
Nsamps = 192000
t = 1×192000
1.0e+00 * 0.0000 0.0000 0.0001 0.0001 0.0001 0.0001 0.0002 0.0002 0.0002 0.0002 0.0002 0.0003 0.0003 0.0003 0.0003 0.0004 0.0004 0.0004 0.0004 0.0005 0.0005 0.0005 0.0005 0.0005 0.0006 0.0006 0.0006 0.0006 0.0007 0.0007
Fc = 1000;
A = 1;
z = A*sin(2*pi*Fc*t);
z(t>StopTime) = 0;
ans = 1×2
1 192000
sound(z,Fs)
Mathieu NOE on 2 Sep 2022
hello again
yeap - finally we converged more or less to the same solution !
still don't forget to normalize mix output to stay within +/- 1 range !
all the best