mixing audios according to array value

2 次查看(过去 30 天)
hi i have the following code which mixes two audios and plots them :
Fs = 44100;
[y,Fs] = audioread("sound and 1000 new.wav");
[x,Fs] = audioread("160 hz.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
Mathieu NOE 2022-9-1
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)
  5 个评论
Mathieu NOE
Mathieu NOE 2022-9-2
ok - got it , simply changed a few things to make sure it works for any number of channels
I added so normalisation of the output otherwise you may clip (wav export assumes your total signal does not exceed +/- 1 range !)
%% my test files
% [y,Fs] = audioread("test_voice_mono.wav"); % mono
[y,Fs] = audioread("test_voice.wav"); % stereo
% Fs = 44100; % why ? - do not overwritte Fs obtained from line above
dt = 1/Fs;
StopTime = 3;
Nsamps = length(y);
t = (1/Fs)*(1:Nsamps)'; % !! NB : transpose t to make it col oriented like y
Fc = 1000;
A = 1;
z = A*sin(2*pi*Fc*t); % is now also col oriented like y
z(t>StopTime) = 0;
mix = y+z;
% important : normalize output amplitude to avoid clipping
mix = mix./(max(abs(mix)));
sound(mix,Fs)
Mathieu NOE
Mathieu NOE 2022-9-2
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Audio I/O and Waveform Generation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by