Problem with data precision while generating square wave.

2 次查看(过去 30 天)
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???

回答(1 个)

Mathieu NOE
Mathieu NOE 2020-11-6
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);

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by