Discretizing functions in Matlab
I have the following function and set of values:
z(t): {R → [-2,3] | z(t) = sin(0.5×π×t) + cos(2×π×t) + 1
t = [-1 : 0.001 : 1]
I need to 开发者_如何转开发determine z(n×Ts) = z(n)
, using the sample period Ts=0.01
, therefore discretizing the fucnction.
I tried using d2d, but for what I've understood can only be applied to zpk functions.
Is there any other way to do it?
If you want a zero order hold approximation of your signal, this can be done by following code:
Ts = 0.01;
t = -1:0.001:1;
n = t./Ts;
nSampled = nan(size(t));
nSampled(1:10:end) = n(1:10:end);
zCont = @(t)(sin(pi*t/2)+cos(2*pi*t)+1);
zZOH = @(n,Ts)(zCont(floor(n).*Ts));
zDisc = @(n,Ts)(zCont(n.*Ts));
figure;
plot(t,zCont(t),'b','DisplayName','Continuous'); hold on;
plot(t,zZOH(n,Ts),'r','DisplayName','ZOH');
stem(t,zDisc(nSampled,Ts),'k','DisplayName','Discrete');
legend('show');
This will give you the output as in the attached figure.
You can try to play with ceil() or round() instead of floor() to get slightly different behavior. If you only need samples at integer values of n, that is something different altogether and is quite different to achieve for the general case (due to roundoff error in floats). However: for your case it will work by simply subsampling the index as is done in nSampled as the subsampling factor is 10. For a non-integer subsampling factor, this will not work properly.
精彩评论