Reply by Umutesi Faith March 19, 20062006-03-19
Ok thanks it helps it understand it this way!

>The idea is that you generate a signal x composed >of three signals x1, x2, x3, each one only active during >a certain amount of time. > >>HTH >Martin > > > > >
Reply by Martin Blume March 19, 20062006-03-19
"Umutesi Faith" schrieb 
> > Yes, thanks a lot finally i could see the 3 > frequencies at different time along the signal which > was exactly what i wanted.
Yes, I thought so from looking at the code.
> the idea here was that when the first wave is > generated so the other ones should be cancelled > (0.0*t2,0.0*t3)and so on. > Did i understand this correctly?
I don't quite understand the sentence, so I can't answer that one. The idea is that you generate a signal x composed of three signals x1, x2, x3, each one only active during a certain amount of time. The important thing to realize is (and this is were you probably went wrong) that the arrays x1,x2,x3 are not indexed by ***time***, but are sequentially numbered starting from 0 (just as plain C arrays are). HTH Martin
Reply by Umutesi Faith March 19, 20062006-03-19
>Does the result of:--- >look more like what you wanted?
Yes, thanks a lot finally i could see the 3 frequencies at different time along the signal which was exactly what i wanted. the idea here was that when the first wave is generated so the other ones should be cancelled (0.0*t2,0.0*t3)and so on. Did i understand this correctly? Anyway this does the right thing! x1 = [sin(2*pi*f1*t1), 0.0*t2, 0.0*t3];
>x2 = [0.0*t1, sin(2*pi*f2*t2), 0.0*t3]; >x3 = [0.0*t1, 0.0*t2, sin(2*pi*f3*t3)];
> >HTH >Martin > >PS: Please, don't top-post. > > >
Thanks for the other suggestion, i could try it some other time..... % your reader may reformat the lines by breaking long lines into shorter ones that matlab will not recognise unless you correct Best of Luck - Mike
>
Reply by Mike Yarwood March 18, 20062006-03-18
"Umutesi Faith" <ma_nz1@yahoo.com> wrote in message 
news:tu2dndyh14Z2vYHZnZ2dnUVZ_sadnZ2d@giganews.com...
> 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 >
% try this Tone1_sampnum=112; Tone2_sampnum=55; nsample=256; phi=zeros(nsample,1); Tone3_sampnum=nsample-Tone2_sampnum-Tone1_sampnum; % should not be -ve so have a bit of sense eh? Tone1_freq= 1188; % Hz Tone2_freq=444; % Hz Tone3_freq=777; % Hz Fs=4000; % samples per second start_time=0 end_time=(nsample-1)/Fs deltaphaseT1=2*pi*Tone1_freq/Fs; deltaphaseT2=2*pi*Tone2_freq/Fs; deltaphaseT3=2*pi*Tone3_freq/Fs; phi(1:Tone1_sampnum)=[0:Tone1_sampnum-1]*deltaphaseT1; phi(Tone1_sampnum+(1:Tone2_sampnum))=phi(Tone1_sampnum)+[1:Tone2_sampnum]*deltaphaseT2; phi(Tone2_sampnum+Tone1_sampnum+(1:Tone3_sampnum))=phi(Tone2_sampnum+Tone1_sampnum)+[1:Tone3_sampnum]*deltaphaseT3; plot(sin(phi)),xlabel('sample number (starts at 1)') figure plot([0:nsample-1]/Fs,sin(phi)),xlabel('time axis (starts at 0)') % your reader may reformat the lines by breaking long lines into shorter ones that matlab will not recognise unless you correct Best of Luck - Mike
Reply by Martin Blume March 18, 20062006-03-18
"Umutesi Faith" schrieb
> > 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. >
Does the result of: t1=( 0 : Ts :(N-200)*Ts ); t2=( (N-200)*Ts : Ts :(N-100)*Ts ); t3=( (N-100)*Ts : Ts : (N-1)*Ts ); x1 = [sin(2*pi*f1*t1), 0.0*t2, 0.0*t3]; x2 = [0.0*t1, sin(2*pi*f2*t2), 0.0*t3]; x3 = [0.0*t1, 0.0*t2, sin(2*pi*f3*t3)]; look more like what you wanted? HTH Martin PS: Please, don't top-post.
Reply by Umutesi Faith March 18, 20062006-03-18
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 > > >
Reply by Martin Blume March 18, 20062006-03-18
"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
Reply by dbell March 17, 20062006-03-17
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>
Reply by Mike Yarwood March 17, 20062006-03-17
"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. >> >> > >
Reply by Umutesi Faith March 17, 20062006-03-17
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. > >