Peter123
Peter123

Reputation: 567

Generating a sine signal with time dependent frequency in Matlab

I want to generate a sine signal y(t) with a time dependent frequency f(t) in Matlab.

I've already tried to realise this using the sine function of Matlab:

h = 0.0001;
npoints = 150/h;
for i = 1:1:npoints
   f(i) = 2 - 0.01*i*h;
   y(i) = 0.5*sin(2*3.1415*f(i)*i*h)+0.5;
end

where the frequency is decreasing with time and h is the time step width.

My problem:

The signal y(t) doesn't look I expected it to look like. There appears a bump in the amplitude at a distinct time (have a look at the plot below).

enter image description here

Does anyone know why this happens and how to generate this sine signal correctly?

Upvotes: 3

Views: 906

Answers (3)

Vahe Tshitoyan
Vahe Tshitoyan

Reputation: 1439

what about

y(i) = 0.5*real(exp(1i*2*pi*f(i)*i*h))+0.5;

You will get the plot below enter image description here

If you just need a chirp signal starting from 2Hz down to 0.5Hz, the following should do the job

f_start = 2; % start frequency
f_end = 0.5; % end frequency
endtime = 150; % seconds
timestep = 0.0001;
times = timestep:timestep:endtime;
y = chirp(times,f_start,endtime,f_end);

and if you plot it you get

figure(2);plot(times,y);

enter image description here

You can achieve the same manually using below

f_start = 2; % start frequency
f_end = 0.5; % end frequency
timestep = 0.0001;
T = 150;
rate_of_change = (f_start - f_end)/T;
times = timestep:timestep:T;
y = sin(2*pi*(f_start*times - times.^2*rate_of_change/2));

It might be useful to read the following Wikipedia page on Chirp signal.

Upvotes: 1

Zizy Archer
Zizy Archer

Reputation: 1390

At 100 you have sin(2*pi*N), which is 0. Change f a little bit, say to 2.0123-... and it goes to the top.

As for the general probably unexpected shape, consider what function you are using in the end (= substitute f back in the formula). You see that you have something of the form y = ...sin(Ai-B*i^2)..., which has a minimum at 100.

The easiest solution here is to simply offset frequency a little more, and use something like f(i) = 3.1 - ..., which has a minimum outside of your considered range.

Upvotes: 1

matiastofteby
matiastofteby

Reputation: 431

It looks like there isn't actually a "bump in frequency", but at the 100 value on the x-axis the entire signal is shifted by 180 degrees. Bear in mind that the amplitude still reaches 0 and does not become smaller (e.g. from 0.25 to 0.75)

This is because the i value becomes so high that the value of f(i) changes sign.

Another indicator of this is that the frequency starts to increase again after the shift instead of gradually becoming even lower.

Why do you start off with the value of 2 in f(i)?

Sorry for asking for clarification here, but I cannot post it as a comment.

Upvotes: 0

Related Questions