Clear Filters
Clear Filters

Problem with data precision while generating square wave.

2 views (last 30 days)
I used square(2*pi*f*t) to generate a square wave.
I set f to 1000 and t=0:Ts:0.05
Ts=1.000000000000000e-05
when i type square(2*pi*1000*0.020500000000000) in command window, answer is +1 and for square(2*pi*1000*0.020510000000000) answer is -1
But when i run a program with the expression square(2*pi*f*t), even for square(2*pi*1000*0.020510000000000) , i am getting +1.
How to fix this???

Answers (1)

Mathieu NOE
Mathieu NOE on 6 Nov 2020
hi
my suggestion
f = 1000 ;
Ts=1e-05;
t=0:Ts:0.05;
angl = 2*pi*(mod(f*t,1));
square_wav = 0.5*(sign(sin(angl))+1);
plot(t,angl);
plot(t,square_wav);

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!