mixing audios according to array value

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 日

0 投票

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 件のコメント

Mohamed Turkmani
Mohamed Turkmani 2022 年 9 月 2 日
they are still incompatible , i wrote
[y,Fs] = audioread("sound and 1000 new.wav");
Fs = 44100;
dt = 1/Fs;
StopTime = 3
Nsamps = length(y);
t = (1/Fs)*(1:Nsamps)
Fc = 1000;
A = 1;
z = A*sin(2*pi*Fc*t);
z(t>StopTime) = 0;
mix = y+z;
sound(mix,Fs)
Mathieu NOE
Mathieu NOE 2022 年 9 月 2 日
hello
can you check size of y and z ?
I suspect they have same number of samples but different orientaion (one is a row vector and the otheroneis a column vector maybe) . In that case, force both of them to be column vectors :
mix = y(:)+z(:);
Mohamed Turkmani
Mohamed Turkmani 2022 年 9 月 2 日
ok i did it i converted the array by
[y,Fs] = audioread("sound and 1000 new.wav");
Fs = 44100;
dt = 1/Fs;
StopTime = 4;
Nsamps = length(y);
t = (1/Fs)*(1:Nsamps);
Fc = 1000;
A = 0.1;
x = A*sin(2*pi*Fc*t);
x = x';
mix = x+y;
sound (mix,Fs)
thanks for your help Matheiu i appreciate it
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 件)

カテゴリ

ヘルプ センター および File ExchangeAudio 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