Shraddha
Shraddha

Reputation: 183

Sinusoidal signal with variable instantaneous frequency

Below is a simple Matlab code that generates a sinusoidal signal with user specified no of cycles. How do I change this such that each cycle has a different instantaneous frequency? For example, if I have 10 cycles, then there should be 10 random frequencies in total where each cycle has a different frequency.

f1 = 120;
fs = f1/60; % sampling frequency depends on f1
no_of_cycles = 10;
f = no_of_cycles/60; % Frequency of sinusoid
duration = 1; % duration of the sinusoidal signal in minutes
A = 1;
t = 1/fs:1/fs:duration*no_of_cycles*1/f; % time index
y1 = A*sin(2*pi*f*t); % simulated sinusoidal signal

Upvotes: 0

Views: 203

Answers (1)

Nicky Mattsson
Nicky Mattsson

Reputation: 3052

Say we have a vector of frequencies, e.g.

frequencies = [1,2,3,4,5];

then we want to make length(frequencies) plots on top of each other, each with a different frequency. In order to make plots on top of each other you can use the command hold on after the plot command.

Additionally we have to keep track of the time, as I suppose you want the next sine curve to continue, where the previous stopped. For this I use variables curTime and endTime to denote the time where the signal starts and ends. Now we have something like:

frequencies = [1,2,3,4,5];
curTime = 0;
for i = 1:length(frequencies)
    endTime = curTime + 2*pi/frequencies(i);
    t = curTime:0.1:endTime;
    plot(t,sin(frequencies(i)*t))
    curTime = endTime;
    hold on
end

However, as we chance the frequency then the y-value of the sine curve which ends is not the same of the one which starts. As I furhter suppose you want it to be continuous, we need to join them, by shifting the "next" sine curve, supposedly by changing $s$ in $sin(ft + s)$. As you are interested in whole cycles we can just chose $s$ such that$ft + s = 0$. Then we end out with:

frequencies = [1,2,3,4,5];
curTime = 0; 
h = 0.01;
for i = 1:length(frequencies)
    endTime = curTime + 2*pi/frequencies(i);
    t = curTime:h:endTime;
    shift = frequencies(i)*t(1);
    plot(t,sin(frequencies(i)*t-shift))
    curTime = endTime;
    hold on
end

Upvotes: 1

Related Questions