user466534
user466534

Reputation:

generate sinusoidal model with time varying frequency

i would like to create sinusoidal time series with time varying frequencies like this . we have observation time from 2.93 second, from 0 to 1 second i want to create sinusoidal data in additive of white noise with one set of frequencies, while from 1 to 2.93 second another set of frequencies, for this i wrote following code

 %   non stationary signal with time varying frequency
fs=100 ; % sampling frequency
ts=1/fs;
t=(0:ts:2.93);  % time vector 
%  i want to create   signal which consist of sinusoidal components in
%  addtiive of white noise ,  frequencies are defined in time as
x=25*sin(2*pi*23*t(t<1))+20*cos(2*pi*24*t(t<1))+24*sin(2*pi*22*t(t<1))+10*randn(size(t));
y=21*sin(2*pi*20*t(t>1))+26*cos(2*pi*17*t(t>1))+21*sin(2*pi*29*t(t>1))+10*randn(size(t));
z=[x';y'];
plot(t',z);

but it shows me error Error using +

Matrix dimensions must agree.

Error in time_frequency (line 7)
x=25*sin(2*pi*23*t(t<1))+20*cos(2*pi*24*t(t<1))+24*sin(2*pi*22*t(t<1))+10*randn(size(t)); 

where i have wrong dimension? also after fixing of this problem is think that code corresponds to situation which i want right?thanks in advance

UPDATED :

%   non stationary signal with time varying frequency
fs=100 ; % sampling frequency
ts=1/fs;
t=(0:ts:2.93);  % time vector 

t1=t(t<1);
t2=t(t>=1);
wn=10*randn(size(t1));
wn1=10*randn(size(t2));
%  i want to create   signal which consist of sinusoidal components in
%  addtiive of white noise ,  frequencies are defined in time as
x=25*sin(2*pi*23*t(t<1))+20*cos(2*pi*24*t(t<1))+24*sin(2*pi*22*t(t<1))+wn;
y=21*sin(2*pi*20*t(t>=1))+26*cos(2*pi*17*t(t>=1))+21*sin(2*pi*29*t(t>=1))+wn1;
z=[x';y'];
plot(t',z);

Upvotes: 0

Views: 765

Answers (2)

danny
danny

Reputation: 306

I separated out the "x=" line to isolate the error:

x1 = 25*sin(2*pi*23*t(t<1));
x2 = 20*cos(2*pi*24*t(t<1));
x3 = 24*sin(2*pi*22*t(t<1));
x4 = 10*randn(size(t(t<1)));

Now x1, x2, x3 and x4 are the same size. You can only sum arrays of the same size. Now this code will not error:

>> x = [x1 + x2 + x3 + x4];
>> plot(t, x)

Then repeat for y, replacing "less than 1" with "greater than or equal to 1".

Upvotes: 2

The Minion
The Minion

Reputation: 1164

your problem is then randn() at the end of your x,y definition. You are trying to add arrays of different dimension. size(t) results in another value than t(t<1) or t(t>1).

Either you could use

x=25*sin(2*pi*23*t(t<1))+20*cos(2*pi*24*t(t<1))+24*sin(2*pi*22*t(t<1))+10*randn(size(t(t<1)));
y=21*sin(2*pi*20*t(t>=1))+26*cos(2*pi*17*t(t>=1))+21*sin(2*pi*29*t(t>=1))+10*randn(size(t(t>=1)));

or you could ignore it and after you define z do:

x=25*sin(2*pi*23*t(t<1))+20*cos(2*pi*24*t(t<1))+24*sin(2*pi*22*t(t<1));
y=21*sin(2*pi*20*t(t>=1))+26*cos(2*pi*17*t(t>=1))+21*sin(2*pi*29*t(t>=1));
z= [x,y];
z=z+10*randn(size(t));

Those should both work.
Another tiny error is that you "delete" your t==1 You must use either t(t<=1) in x or t(t>=1) in y definition or the dimension of z and t won't be the same

Upvotes: 1

Related Questions