# First-order hold interpolation in matlab

I have a sinusoidal signal x(t) sampled with period 1/fs. The result is the discrete signal x[n], where x[n]=x(t)|t=nT. Then I perform an upsampling of factor, let's say, 8 to increase the sampling frequency by a factor of 8. Here's the code in matlab syntax:

f=10e6; % signal's frequency
fs=1e9; % sampling frequency
t=0:1/fs:20/f1-1/fs; % time vector
x=sin(2*pi*f*t); % sinusoidal signal

% upsampling by a factor of 8
x1=upsample(x,8);
% filtering
filt=[1,2,1,2,1,2,1,2,1]/2; %triangular impulse response
foh=conv(x1,filt,'same');

%scale time axis
nt=0:1/(8*fs):20/f-1/(8*fs); % scaled time vector

%plot reconstructed signal
plot(nt,foh)


Now after running the above code I get the following plot as the output of the reconstructed signal, which doesn't appear to be a sinusoidal anymore, This more looks like a predictive first-order hold than a basic one. There seems to be something wrong with my filtering scheme but don't know how to fix that to get a basic FOH response.

Thanks..

Your filt is simply not a first-order hold filter.

Fix that by writing down what each sample in your 7 inter-original-sample samples need to have as value. You'll come up with a description like:

The first inserted sample needs to be the sum of the last sample, and the 1/8 of the difference to the next input sample.

That is a simple linear equation:

\begin{align} y[n=m+1] &= x[m] + (x[m+8]-x[m])\cdot\frac 18\\ &= x[n-1] + \frac18 x[n+7] + (-\frac18)x[n-1]\\ &= \frac{8-1}8 x[n-1] + \frac18 x[n+7] \end{align}

notice where the the $1$ from the index on the left side reappears on the right side.

Do the same for the second, third, … seventh sample that you insert.

You'll notice that the vector you get bears no resemblance to your filt coefficients!

• I actually want to correct the filter filt and do the convolution with x[n] to reconstruct the signal. What you'e suggesting is ok but not like what I asked May 19 '19 at 19:52
• Please try to do what I recommend in my answer: compare your convolution with the vector filt with the coefficients you get by doing above considerations for the interpolated values. My answer addresses exactly what you address in your comment! May 19 '19 at 20:01
• I have to insert 7 zeros after each sample. There are say N samples. So should I write down y[n] for 7N samples? The other thing I don't get from your answer is as to how the data vector y[n] would lead to the coefficients of filt. Also what does index m indicate? Sorry I'm new to signal processing.. Still have got a lot to learn May 19 '19 at 20:34
• yes, write down that y for the seven interpolated samples :) Then, write down what the convolution with filt means, i.e. imagine your filter would consists of a coefficient series h[n], and simply write down what, for example, y[n] would look like for n=m+4 (with that I meant "the fourth inserted sample"). May 20 '19 at 5:15