Forums

Sine signal with varied frequency in a matlab M-file

Started by mariya March 17, 2006
Hello

I am trying to generate a sine signal with varied frequencies in a matlab
m-file and here is the piece the code that i am using. This gives me error
and i cannot figure out what went wrong. i hope those familiar with " the
for loop statements in matlab" can give me some hints

Thank you in advance!

(sorry if this message is posted twice, i was not sure if the first one
reached its destination)

clear ;
close all;
Fs=4000;% sampling frequency
Ts=1/Fs;%sampling interval
N=256;%number of samples
f1=800; %signal frequency
f2=420;
f3=310;
t=(0:Ts:(N-1)*Ts);
for t1= 0:(N-200)*Ts
    x1 = sin(2*pi*f1*t);
for t2 = (N-200)*Ts:(N-100)*Ts
    x2= sin(2*pi*f2*t);
for t3= (N-100)*Ts:(N-1)*Ts
    x3 = sin(2*pi*f3*t);
    END
   END
 END
x = x1+x2+x3;
figure, plot(x)
mariya wrote:
> Hello > > I am trying to generate a sine signal with varied frequencies in a matlab > m-file and here is the piece the code that i am using.
Do you want to simulate an FM signal, or do you want to create a signal consisting of three different frequencies at three distinct periods in time (almost like FSK)?
> x = x1+x2+x3;
What you have done is to linearly sum three frequencies. Kind Regards, Jaco
mariya wrote:
> Hello > > I am trying to generate a sine signal with varied frequencies in a matlab > m-file and here is the piece the code that i am using. This gives me error > and i cannot figure out what went wrong. i hope those familiar with " the > for loop statements in matlab" can give me some hints > > Thank you in advance! > > (sorry if this message is posted twice, i was not sure if the first one > reached its destination) > > clear ; > close all; > Fs=4000;% sampling frequency > Ts=1/Fs;%sampling interval > N=256;%number of samples > f1=800; %signal frequency > f2=420; > f3=310; > t=(0:Ts:(N-1)*Ts); > for t1= 0:(N-200)*Ts > x1 = sin(2*pi*f1*t); > for t2 = (N-200)*Ts:(N-100)*Ts > x2= sin(2*pi*f2*t); > for t3= (N-100)*Ts:(N-1)*Ts > x3 = sin(2*pi*f3*t); > END > END > END
try "end" rather then "END"
> x = x1+x2+x3; > figure, plot(x)
thanks for the reply,

I don't know if i can call this FM ( frequency modulation) but what i want
to do is the generate a signal composed of 3 frequencies, i was just trying
to do it with this matlab m-file but it does not seem to work!
By doing x = x1+x2+x3, i was hoping to have all these 3 frequencies in the
same the plot?

Do you think i was using the correct methods?
Any suggestions?

Thanks


>mariya wrote: >> Hello >> >> I am trying to generate a sine signal with varied frequencies in a
matlab
>> m-file and here is the piece the code that i am using. > >Do you want to simulate an FM signal, or do you want to create a signal >consisting of three different frequencies at three distinct periods in >time (almost like FSK)? > > >> x = x1+x2+x3; > >What you have done is to linearly sum three frequencies. > >Kind Regards, >Jaco > >
If you only want to put the 3 frequencies in the same figure, try:
t1= 0:(N-200)*Ts;
t2 = (N-200)*Ts:(N-100)*Ts ;
t3= (N-100)*Ts:(N-1)*Ts ;
plot(t1,x1,'r',t2,x2,'g',t3,x3,'b')
If you want the superposition of these three frequencies, I think what
you did is correct.

I have just tried this piece of code but i just get a blank plot?
clear ;
close all;
Fs=4000;% sampling frequency
Ts=1/Fs;%sampling interval
N=256;%number of samples
f1=800; %signal frequency
f2=420;
f3=310;
t=(0:Ts:(N-1)*Ts);
t1= 0:(N-200)*Ts;
t2 =(N-200)*Ts:(N-100)*Ts;
t3= (N-100)*Ts:(N-1)*Ts;
x1= sin(2*pi*f1*t1);
x2= sin(2*pi*f2*t2);
x3= sin(2*pi*f3*t2);
x = x1+x2+x3;
%figure, plot(x)
plot(t1,x1,'r',t2,x2,'g',t3,x3,'b')% actually was the idea here to 
get these on 3 different plots?

The other thing is that i am hesitating about the interval between the
samples because on the plot it seems that i don't get 256 samples. It runs
from 0 to 0.04 quite strange!

thanks if u have any other suggestions!


//


f you only want to put the 3 frequencies in the same figure, try:
>t1= 0:(N-200)*Ts; >t2 = (N-200)*Ts:(N-100)*Ts ; >t3= (N-100)*Ts:(N-1)*Ts ; >plot(t1,x1,'r',t2,x2,'g',t3,x3,'b') >If you want the superposition of these three frequencies, I think what >you did is correct. > >
"Umutesi Faith" <ma_nz1@yahoo.com> wrote in message 
news:v4mdnd6CkrgEkYbZnZ2dnUVZ_v6dnZ2d@giganews.com...
>I have just tried this piece of code but i just get a blank plot?
You should get a single dot per trace because you have one point per trace : try making your t1, t2 and t3 vectors longer than 1 best of luck - Mike
> clear ; > close all; > Fs=4000;% sampling frequency > Ts=1/Fs;%sampling interval > N=256;%number of samples > f1=800; %signal frequency > f2=420; > f3=310; > t=(0:Ts:(N-1)*Ts); > t1= 0:(N-200)*Ts; > t2 =(N-200)*Ts:(N-100)*Ts; > t3= (N-100)*Ts:(N-1)*Ts; > x1= sin(2*pi*f1*t1); > x2= sin(2*pi*f2*t2); > x3= sin(2*pi*f3*t2); > x = x1+x2+x3; > %figure, plot(x) > plot(t1,x1,'r',t2,x2,'g',t3,x3,'b')% actually was the idea here to > get these on 3 different plots? > > The other thing is that i am hesitating about the interval between the > samples because on the plot it seems that i don't get 256 samples. It runs > from 0 to 0.04 quite strange! > > thanks if u have any other suggestions! > > > // > > > f you only want to put the 3 frequencies in the same figure, try: >>t1= 0:(N-200)*Ts; >>t2 = (N-200)*Ts:(N-100)*Ts ; >>t3= (N-100)*Ts:(N-1)*Ts ; >>plot(t1,x1,'r',t2,x2,'g',t3,x3,'b') >>If you want the superposition of these three frequencies, I think what >>you did is correct. >> >> > >
In the original code, running through the loops more than 1 time would
make no difference, since nothing inside the loop depends on the loop
parameter. It would just assign the same x1, x2, x3 over and over.

Dirk


Mike Yarwood wrote:
> "Umutesi Faith" <ma_nz1@yahoo.com> wrote in message > news:v4mdnd6CkrgEkYbZnZ2dnUVZ_v6dnZ2d@giganews.com... > >I have just tried this piece of code but i just get a blank plot? > You should get a single dot per trace because you have one point per trace > : try making your t1, t2 and t3 vectors longer than 1 > > best of luck - Mike >
<snipped>
"mariya" schrieb
> > I am trying to generate a sine signal with varied > frequencies in a matlab m-file and here is the piece > the code that i am using. This gives me error > and i cannot figure out what went wrong. i hope those > familiar with " the for loop statements in matlab" can > give me some hints > > Thank you in advance! > > [...] > t=(0:Ts:(N-1)*Ts);
t is an array of length N
> for t1= 0:(N-200)*Ts > x1 = sin(2*pi*f1*t);
Why a for loop? Just with x1=sin(w*t); you are creating an array of length N, because t is already an array of length N. With the for loop you are just recreating the x1 array again and again. And then a question: how many times is the for loop executed in your opinion?
> for t2 = (N-200)*Ts:(N-100)*Ts > x2= sin(2*pi*f2*t); > for t3= (N-100)*Ts:(N-1)*Ts > x3 = sin(2*pi*f3*t); > END > END > END
Why three nested for loops? t=(0:Ts:(N-1)*Ts); x1 = sin(2*pi*f1*t); x2 = sin(2*pi*f2*t); x3 = sin(2*pi*f3*t); x = x1+x2+x3; figure, plot(x) is equivalent to your code and IMHO much cleaner. I am not sure what you wanted to do in the first place: a.) a sum of three frequencies from 0<=t<(N-1)*Ts? This is what the code (after changing END to end) does. b.) f1 active from 0<=t<(N-200)*Ts, f2 active from (N-200)*Ts<=t<(N-100)*Ts, f3 active from (N-100)*Ts<=t<(N-1)*Ts? This is what a very cursory reading of the code to a human reader seems to suggest (well, at least to me). HTH Martin
hi

The idea using the for loop was to get these frequencies at different
time, i did not have any other way on how to get these frequencies. Using
the following code 
t=(0:Ts:(N-1)*Ts);
x1 = sin(2*pi*f1*t);
x2 = sin(2*pi*f2*t);
x3 = sin(2*pi*f3*t);
x = x1+x2+x3;
figure, plot(x)

i will just get these 3 frequencies but they exist at all time in the
signal. This is the reason why i was trying to use t1, t2 and t3 and
unfortunatly it seems no to do the work! 

Any suggestions
thanks




>"mariya" schrieb >> >> I am trying to generate a sine signal with varied >> frequencies in a matlab m-file and here is the piece >> the code that i am using. This gives me error >> and i cannot figure out what went wrong. i hope those >> familiar with " the for loop statements in matlab" can >> give me some hints >> >> Thank you in advance! >> >> [...] >> t=(0:Ts:(N-1)*Ts); >t is an array of length N > >> for t1= 0:(N-200)*Ts >> x1 = sin(2*pi*f1*t); >Why a for loop? >Just with x1=sin(w*t); you are creating an array of >length N, because t is already an array of length N. >With the for loop you are just recreating the x1 array >again and again. > >And then a question: how many times is the for loop executed in your >opinion? > >> for t2 = (N-200)*Ts:(N-100)*Ts >> x2= sin(2*pi*f2*t); >> for t3= (N-100)*Ts:(N-1)*Ts >> x3 = sin(2*pi*f3*t); >> END >> END >> END >Why three nested for loops? > >t=(0:Ts:(N-1)*Ts); >x1 = sin(2*pi*f1*t); >x2 = sin(2*pi*f2*t); >x3 = sin(2*pi*f3*t); >x = x1+x2+x3; >figure, plot(x) > >is equivalent to your code and IMHO much cleaner. > >I am not sure what you wanted to do in the first place: >a.) a sum of three frequencies from 0<=t<(N-1)*Ts? >This is what the code (after changing END to end) does. >b.) f1 active from 0<=t<(N-200)*Ts, >f2 active from (N-200)*Ts<=t<(N-100)*Ts, >f3 active from (N-100)*Ts<=t<(N-1)*Ts? >This is what a very cursory reading of the code to a >human reader seems to suggest (well, at least to me). > >HTH >Martin > > >