Im trying to generate a sweep sine wave that increases its frequency till an upper limit and then starts decreasing its frequency to the lower limit at the same rate. Im trying to simulate in matlab.

The frequency of the wave will change based on the following formula: f = fmax-a*abs(T - t); a in the formula equals (fmax-fmin)/T So the frequency should first increase and then decrease after it reaches time T at the same rate.

The matlab code is:

freq1 = 20;
freq2 = 200;
fs = 44100;
endTime = 0.1;
t = 1/fs:1/fs:(endTime*2);
a = (freq2-freq1)/endTime;
f = freq2 -   a*abs(endTime - t);
sinwave = sin(2*pi*f.*t);

It seems to display a very different result when we plot the value obtained. After T the plot suddenly reverses, changes to original value of frequency and again increases in frequency I am not able to understand this behavior as the frequency seems to give values that increase and then decreases. Please help me understand why the plot for waves shows a different value and how to correct this. (Sorry not able to include plot due to web site restrictions)


migrated from stackoverflow.com May 9 '14 at 14:29

This question came from our site for professional and enthusiast programmers.


I think that in your simulation, you only calculate the instantaneous frequency, but then fail to compute the phase for the sin() function correctly.

This is how you could correct this issue:

% parameters of linear up/down-sweep
freq1 = 20;
freq2 = 200;
fs = 44100;

% length of a sweep in one direction
endTime = 0.1;

% number of samples of a sweep in one sweep direction
N = endTime * fs;

% instantaneous frequency at each point in time ...
% ... first increasing for N points, then decreasing for N points
inst_f = [linspace(freq1, freq2, N) linspace(freq2, freq1, N)];

% since (in continuous time) instantaneous frequency is derivative of
% phase, we have to compute the "integral" to get the phase for sin().
phi = 2 * pi * cumsum(inst_f) / fs;
sweep = sin(phi);

% plotting
t = linspace(0, endTime * 2, length(s));
plot(t, sweep);

% wavwrite(sweep, fs, 32, '~/sweep_delete.wav');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy