I have a signal $x(t) = \sin(2\pi t)$ with $t \in [0,1]$. The signal $x_s(t)$ is the sampled signal with a sampling period of $T_s = 0.05$. Now I want to compute the reconstructed $x_r(t)$ signal using a zero-order-hold.
The ZOH has the following impulse response
$$h_{\mathrm{ZOH}}(t,T_s)\,= \frac{1}{T_s} \mathrm{rect} \left(\frac{t}{T_s}-\frac{1}{2} \right)$$
This I implemented in Matlab:
zohImpl = @(t,Ts) 1/Ts*rectangularPulse(t/Ts - 1/2); Now I simply did a convolution between the impulse response $h_{\mathrm{ZOH}}$ and the sampled signal $x_s(t)$
xZoh = conv(zohImpl(t,Ts),xSampled);
However I do not get the result expected, the reconstructed signal. I get a sinus with a higher amplitude and in the convolution, xZoh gets signal points padded which are 0.
What am I doing wrong here? I would namely expect the same results as which I obtain using...
k = 0; for i = 1:length(t) if k*Ts <= t(i) && t(i) < (k + 1)*Ts k = k + 1; end xZoh(i) = xSampled(k); end Full code:
clear all; close all; clc; % Impulse ZOH zohImpl = @(t,Ts) 1/Ts*rectangularPulse(t/Ts - 1/2); % Impulse FOH fohImpl = @(t,Ts) 1/Ts*triangularPulse(t/Ts); % Impulse FOH delayed fohDelayImp = @(t,Ts) 1/Ts*triangularPulse((t - Ts)/Ts); % Impulse FOH predictive fohPreImp = @(t,Ts) 1/Ts*(rectangularPulse(t/Ts - 1/2) - ... rectangularPulse(t/Ts - 3/2) + ... triangularPulse(t/Ts - 1)); % Time domain tStart = 0; tStep = 1e-4; tEnd = 1; t = tStart:tStep:tEnd; % Original signal x = @(t)(sin(2*pi*t)); % Sampling period Ts = 5e-2; % Samples nSamples = tEnd/Ts; samples = 0:nSamples; % Sampled signal xSampled = x(samples*Ts); % Convolution with impulse response xZoh1 = conv(zohImpl(t,Ts),xSampled); % Plot figure(4); hold all; plot(t,x(t)); stem(samples*Ts,xSampled); % plot(t,xZoh1); does not work k = 0; for i = 1:length(t) if k*Ts <= t(i) && t(i) < (k + 1)*Ts k = k + 1; end xZoh2(i) = xSampled(k); end plot(t,xZoh2); Created signal using convolution (plot(xZoh1)): 
Created signal using for loop (plot(xZoh2)): 
latest code
clear all; close all; clc; % Time domain tStart = 0; tStep = 1e-4; tEnd = 1; t = tStart:tStep:tEnd; beta = 0.2; % Original signal x = @(t)(sin(2*pi*t)); % Sampling period Ts = 5e-2; % Impulse ZOH zohImpl = @(t,Ts) 1/Ts*rectangularPulse(t/Ts - 1/2); % Impulse FOH fohImpl = @(t,Ts) 1/Ts*triangularPulse(t/Ts); % Impulse FOH delayed fohDelayImp = @(t,Ts) 1/Ts*triangularPulse((t - Ts)/Ts); % Impulse FOH predictive fohPreImp = @(t,Ts) 1/Ts*(rectangularPulse(t/Ts - 1/2) - ... rectangularPulse(t/Ts - 3/2) + ... triangularPulse(t/Ts - 1)); % Impulse FOH fractional fohFractImp = @(t,Ts,beta) 1/Ts*(rectangularPulse(t/Ts - 1/2) - ... beta*rectangularPulse(t/Ts - 3/2) + ... beta* triangularPulse(t/Ts - 1)); tt = -1:1e-4:1; figure(1); subplot(5,1,1); plot(tt,zohImpl(tt,Ts)); grid on; box on; axis tight; subplot(5,1,2); plot(tt,fohImpl(tt,Ts)); grid on; box on; axis tight; subplot(5,1,3); plot(tt,fohDelayImp(tt,Ts)); grid on; box on; axis tight; subplot(5,1,4); plot(tt,fohPreImp(tt,Ts)); grid on; box on; axis tight; subplot(5,1,5); plot(tt,fohFractImp(tt,Ts,beta)); grid on; box on; axis tight; % Samples nSamples = tEnd/Ts; samples = 0:nSamples; % Sampled signal xSampled = zeros(1,length(t)); xSampled(1:Ts/tStep:end) = x(samples*Ts); % Convolution with impulse response xZoh = conv(zohImpl(t,Ts),xSampled); xFoh = conv(fohImpl(t-t(Ts/tStep),Ts),xSampled); xFohDelayImp = conv(fohDelayImp(t,Ts),xSampled); xFohPreImp = conv(fohPreImp(t,Ts),xSampled); xFohFractImp = conv(fohFractImp(t,Ts,beta),xSampled); xZoh = xZoh(1:length(t)) / nSamples; xFoh = xFoh(1:length(t)) / nSamples; xFohDelayImp = xFohDelayImp(1:length(t)) / nSamples; xFohPreImp = xFohPreImp(1:length(t)) / nSamples; xFohFractImp = xFohFractImp(1:length(t)) / nSamples; % Plot figure(2); hold all; plot(t,x(t)); % stem(t,xSampled); plot(t,xZoh); plot(t-t(Ts/tStep),xFoh); plot(t,xFohDelayImp); plot(t,xFohPreImp); plot(t,xFohFractImp); 