How to create a Stereo Sweep from Frequency A to B in a given time?

12 次查看(过去 30 天)
Dear MathWorks-Community,
I would like to create an audio sweep from frequency A to B in a given time:
For example from 400 to 600 Hertz in 1 second.
Or is it even possible to create an audio sweep from A to B in a given time and then hold this frequency for a given time?
For example from 400 to 600 Hertz in 1 second and the hold 600 Hertz for 9 seconds
Thank you for helping it is much appreciated!

采纳的回答

Star Strider
Star Strider 2021-12-6
The built-in Signal Processing Toolbox chirp function is perfect for this —
Fs = 1E+4;
t1 = linspace(0, Fs-1, Fs)/Fs;
s1 = chirp(t1,400,t1(end),600,'linear'); % Linearly-Increasing Signal
t2 = linspace(t1(end), 9*Fs-1, 9*Fs)/Fs;
s2 = cos(2*pi*600*t2); % Conmstant Signal
t = [t1 t2]; % Concatenate
s = [s1 s2]; % Concatenate
[sp,fp,tp] = pspectrum(s,Fs,'spectrogram','TimeResolution',0.05); % Display Results
figure
mesh(tp,fp,sp)
grid on
xlabel('Time (s)')
ylabel('Frequency (Hz)')
view(60,60)
ylim([0 1000])
To actualy llisten to it —
player = audioplayer(s, Fs);
play(player)
.
  1 个评论
Star Strider
Star Strider 2021-12-6
Following up —
To write a .wav file, use the audiowrite function —
audiowrite('filename.wav', s, Fs)
using the appropriate file name. To save it as a stereophonic file, use ‘smtx’ (see below) instead.
Changing the amplitude requires multiplying it by an appropriate vector with values in the range of ±1. So to create a stereo effect —
inc = linspace(0, 1, numel(s));
dec = linspace(1, 0, numel(s));
smtx = [s(:).*dec(:) s(:).*inc(:)]; % Stereo Matrix [Left Right]
sound(smtx,Fs) % Listening
I cannot hear much of a difference, however the code works in theory —
figure
plot(t, smtx)
grid
xlabel('Time')
ylabel('Amplitude')
legend('Left Channel','Right Channel', 'Location','best')
.

请先登录,再进行评论。

更多回答(1 个)

Jon
Jon 2021-12-6
As an alternative, if you don't have the signal processing toolbox, or for some reason preferred to see the details you could do it like this:
% define time and frequency breakpoints
tb = [0 1 9]
fb = [400 600 600]
% compute swept frequency
tFinal = max(tb)
fSample = 10000; % sampling frequency
t = linspace(0,tFinal,fSample*tFinal);
f = interp1(tb,fb,t); % linearly interpolate frequencies to get sweep
% compute output
y = sin(f*2*pi.*t);
% plot result
plot(t,y)
xlabel('time [s]')
ylabel('signal')
  1 个评论
Jon
Jon 2021-12-6
Note, this is quite general, you could describe any pattern of sweeps and holds using the time and frequency breakpoints tb, and fb

请先登录,再进行评论。

标签

Community Treasure Hunt

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

Start Hunting!

Translated by