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

15 views (last 30 days)
Hans Buchele on 6 Dec 2021
Commented: Star Strider on 6 Dec 2021
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 on 6 Dec 2021
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)
.
Star Strider on 6 Dec 2021
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')
.

Jon on 6 Dec 2021
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')
Jon on 6 Dec 2021
Note, this is quite general, you could describe any pattern of sweeps and holds using the time and frequency breakpoints tb, and fb