DSPRelated.com
Forums

Sine signal with varied frequency in a matlab M-file

Started by mariya March 17, 2006
"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.
"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
>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
>
"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
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 > > > > >