I am trying to generate a sawtooth waveform between 0 and 1 which would have a frequency upto 200 kHz. I run this piece of code within a Matlab function block - it works fine up to 10 kHz, but higher than that frequency it doesn't give the desired waveform.
For example, at F = 40 kHz, I get the right shape but it's peak is not at 1 anymore. At 50 kHz, I do not get a clean sawtooth wave anymore where all the triangles are between 0 and 1, it jumps up and gets shifted up a little every now and then (please see pic.2).
Can someone tell me where I'm going wrong in my function?
function y = fcn(clk)
T = 1/40e3;
if(mod(clk,T)==0)
y = 0;
else
y = (clk-floor(clk/T)*T)/T;
end
mod()
function then you don't need thex-floor(x)
since that's how it's implemented (or something like that). Just use the functionmod()
, directly. \$\endgroup\$mod()
, directly? \$\endgroup\$