Square Wave Generation becomes inaccurate !
5 views (last 30 days)
Show older comments
Hi there! can someone help me please ? I have a problem with this here. When I generate the square wave after a while it becomes inaccurate! I need this Period:[1 1 1 1 -1 -1 -1 -1], N times. But it makes somewhere a mistake and I have after some Periods three times 1 or five times -1 and so on!
and I need help to make a square wave like this ->
Period:[1 1 -1 -1 -1 -1 1 1], N times, too.
Thanks a lot.
f_r=4;
spp = 8; % Samples per Period
N=1000;
t = 0:1/(spp*f_r):N-1/(spp*f_r); % (time)
f_Sq = 4;
A_Sq = 1;
W_Sq = floor(2*pi*f_Sq);
S_Sq = A_Sq*square(t*W_Sq);
0 Comments
Accepted Answer
dpb
on 9 Mar 2015
The best square can do is to have the requested duty cycle that most nearly approximates that requested over the length of the series. 2*pi*f_Sq isn't going to be exact and using floor on it passing that to square will make it shorter than the average would otherwise be. The only way you can get an absolute repeat of N cycles of a given number of +/- values will be to either have the argument exact or to actually replicate the cycle N times and then scale that by a time duration.
3 Comments
dpb
on 10 Mar 2015
Interesting...after some fooling around here square doesn't seem to work very well, indeed; I've never used it much so hadn't really counted the number or spacing exactly. Unfortunately, even with an evenly-divisible time vector and using a 50% duty cycle one gets
>> t = 0:1/(spp*f_r):N-1/(spp*f_r);
>> length(t)
ans =
32000
Indeed, is divisible exactly by 8 so should be able to get a perfect result...
>> sq=square(t,50);
>> [sum(sq==1) sum(sq==-1)]
ans =
16016 15984
>> ans(1)/length(sq)*100
ans =
50.0500
>>
But it isn't...
Also, you can't fix the frequency this way to try to fix the length of the +/- sections, the internal logic ends up with
>> diff(find(abs(diff(sq)==2)))
ans =
Columns 1 through 15
201 201 201 201 201 201 201 201 201 201 201 201 201 201 201
Columns 16 through 30
202 201 201 201 201 201 201 201 201 201 201 201 201 201 201
Columns 31 through 45
201 202 201 201 201 201 201 201 201 201 201 201 201 201 201
Columns 46 through 60
201 201 202 201 201 201 201 201 201 201 201 201 201 201 201
Columns 61 through 75
201 201 201 202 201 201 201 201 201 201 201 201 201 201 201
Columns 76 through 90
201 201 201 201 202 201 201 201 201 201 201 201 201 201 201
Columns 91 through 105
201 201 201 201 201 202 201 201 201 201 201 201 201 201 201
Columns 106 through 120
201 201 201 201 201 201 201 202 201 201 201 201 201 201 201
Columns 121 through 135
201 201 201 201 201 201 201 201 202 201 201 201 201 201 201
Columns 136 through 150
201 201 201 201 201 201 201 201 201 202 201 201 201 201 201
Columns 151 through 158
201 201 201 201 201 201 201 201
>>
the sections are 201 long in the main w/ the occasional 202...
To build a specific series, build the series subsection and replicate it--
>> w=[repmat(1,1,4) repmat(-1,1,4)];
>> sq=repmat(w,1,length(t)/length(w));
>> [sum(sq==1) sum(sq==-1)]
ans =
16000 16000
>>
This will, of course, have a much higher frequency content than that of the other; you'll have to work that out as you can't fix the number of the length of each +/- section and the time independently; they're correlated.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!