Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Generation of data according to sine function with varying period

6 views
Skip to first unread message

Tanveer Awal

unread,
Feb 10, 2012, 8:03:09 AM2/10/12
to
I want to plot sine wave with variation of period specified by the periods array(60,60,50,40,30,30,20,10s -> for total 300 second) sequentially. Although the following code plots sine wave with amplitude delta, when the period changes, sometimes its phase changes unexpectedly. Please help me to find out the problem
as soon as possible and let me know how to plot as I want. The code that I used is as follows.

simTime = 300;
periods=[60,60,50,40,30,30,20,10];
selector=0; //for looping through the periods
j=1; //counter
t=1; //increment step
delta=5; %amplitude
for i = 0:t:simTime
Vf=delta*sin(2*pi*i/periods(selector+1));
dvalue=0;
for tempo = 0:1:(selector-1)
dvalue=dvalue+periods(tempo+1);
end
if(i>dvalue && mod((i-dvalue),periods(selector+1))<=0)
selector=selector+1;
end
VfList(j)=Vf;
timeList(j)=i;
j=j+1;
end

figure;
hold;
grid on;
plot(timeList,VfList,'r');

dpb

unread,
Feb 10, 2012, 1:51:26 PM2/10/12
to
On 2/10/2012 7:03 AM, Tanveer Awal wrote:
> I want to plot sine wave with variation of period specified by the
> periods array(60,60,50,40,30,30,20,10s -> for total 300 second)
> sequentially. Although the following code plots sine wave with amplitude
> delta, when the period changes, sometimes its phase changes
> unexpectedly. Please help me to find out the problem
...

Don't have time (or more accurately, inclination) to try to debug your
code specifically but what you need is to find the proper phase shift to
match the quadrant and point in the previous frequency and the new
frequency that have the same magnitude and slope at the junction point
instead of just the magnitude difference you're doing. As you can see,
you're picking up in the wrong quadrant even though you've matched the
amplitude by what making an additive delta; that's not sufficient in of
itself.

Descriptively, you need theta1 and theta2 where theta1 is the present
phase angle at the end of the present segment and then find theta2 such
that it is in both the same quadrant and that inverse sine of that new
phase angle is determined by the magnitude.

--

Tanveer Awal

unread,
Feb 11, 2012, 12:15:28 AM2/11/12
to
dpb <no...@non.net> wrote in message <jh3ovb$bvn$1...@speranza.aioe.org>...
I could not understand you clearly. However,I used your clue to change the code as follows but it still it does not work. Could anybody please help? Providing the required code is better for me.

simTime = 300;
periods=[60,60,50,40,30,30,20,10];
selector=0;
j=1;
t=1;
delta=5;
flag=0;

dif=0;
for i = 0:t:simTime
if flag
oldval=sin(2*pi*i/periods(selector));
newval=sin(2*pi*i/periods(selector+1))
dif=asin(newval)-asin(oldval);
end
flag=0;
Vf=delta*sin(2*pi*i/periods(selector+1)+dif);
dvalue=0;
for tempo = 0:1:(selector-1)
dvalue=dvalue+periods(tempo+1);
end
if(i>dvalue && mod((i-dvalue),abs(periods(selector+1)))<=0)
selector=selector+1;
flag=1;

dpb

unread,
Feb 11, 2012, 10:45:03 AM2/11/12
to
On 2/10/2012 11:15 PM, Tanveer Awal wrote:
...

> I could not understand you clearly. However,I used your clue to change
> the code as follows but it still it does not work. Could anybody please
> help? Providing the required code is better for me.
....

Against my better judgment, try this...

periods=[60,60,50,40,30,30,20,10];
delta=5;
v=[];
for idx = 1:length(periods)
t=2*pi*[0:periods(idx)]/periods(idx);
v=[v delta*sin(t)];
end

figure;
hold;
grid on;
plot([1:length(v)],v,'r');

--

dpb

unread,
Feb 11, 2012, 12:43:11 PM2/11/12
to
On 2/11/2012 9:45 AM, dpb wrote:
...

> periods=[60,60,50,40,30,30,20,10];
> delta=5;
> v=[];
> for idx = 1:length(periods)
> t=2*pi*[0:periods(idx)]/periods(idx);
...

BTW, you undoubtedly will need much better resolution than this;
particularly as the period gets shorter. Note the shape of the graph as
your frequency gets higher.

I'll leave as exercise for the student dealing w/ that detail.

Also, this (as yours) will have a discontinuity in the generated
waveform at the points the frequency changes given that it is a step
change. Plot diff(v) on top of the plot as well to see how bad this
is/becomes.

To solve this easily, if you have the Signal Processing Toolbox,

doc chirp

else't lookup up "swept-frequency generator" to see techniques for same...

--

Tanveer Awal

unread,
Feb 13, 2012, 3:52:11 AM2/13/12
to
dpb <no...@non.net> wrote in message <jh62dr$goo$1...@speranza.aioe.org>...
Thanks for the code. It is helpful.
0 new messages