I have an exercise to write a function which upsamples or downsamples a signal by a factor of F (integer) using a FIR filter windowed with a Kaiser window function. Exercise I had last week included us to write a function which returns an impulse response of such filter so we can use it in this exercise rather than using a built-in function from MATLAB.

The function h = firkaiser(Wc, Bt, As, Ap) creates a NF filter with central frequency Wc, Bt which is a band where the filter cuts off, and attenuations in the stopband and passband. Using a standard procedure of calculating M (length of impulse response) and windowing an ideal filter i have created this function. Here is an example of a NF FIR filter created by this function just to make sure the problem isn't in the filter itself.

h = firkaiser(0.3*pi, 0.3, 50, 0.5)

Gives these results: Impulse response NF FIR FILTER

Now to the problem. In the exercise description as a hint it is mentioned that the interpolaton filters used must have Wc = pi/F, Bt = 0.4*pi/F, As = 70dB and Ap = 0.85dB so we must use these values. We also must compensate the delay so that the output signal is in phase with the input signal. Here is the function i wrote:

function [y] = resamplesignal(x, sampling, F)

if (sampling(1) == 'u') %if you want to upsample
    xr = upsample(x,F);
    fir = firkaiser(pi/F, 0.4*pi/F, 70, 0.85);
    y = filter(fir, 1, xr);
    delay = mean(grpdelay(fir)); %removing the delay
    y(1:delay) = [];
    y = F*y; %upsampling lowers the amplitudes

    stem(1:F:F*length(x), x);
    hold on
    fir = firkaiser(pi/F, 0.4*pi/F, 70, 0.85);
    xr = filter(fir, 1, x);
    delay = mean(grpdelay(fir)); %removing the delay
    xr(1:delay) = [];
    y = downsample(xr, F);

    hold on
    stem(1:F:F*length(y), y);
close all

The function works as expected but the last part of the signal is lost first because of the delay and when the delay is deleted it's just missing from the signal. Here is an example where i wanted to upsample F=2 a cosine function: Upsampled and Original signal

The original signal is the plotted blue one and the upsampled is the red stemmed one. The last part of the signal is lost. The same goes for the downsampling by let's say F=5: Downsampled and Original signal

The blue one is again the original signal. In the first picture i needed to stretch the original so as to have a proper comparison and the same for the downsampled in the second picture. How can i fix this? Or is it fixable at all?

  • $\begingroup$ First of all, I'm not sure you should use the average of the group delay. The group delay outside the passband (i.e. where gain is roughly 1), will not be a constant. You should be able to determine the group delay easily if your filter is symmetric, It should group delay = (Number of taps - 1)/2 Second of all, as you shift the signal to remove the group delay, you will lose samples... That's why you're missing samples.... $\endgroup$ – Ben Feb 3 '19 at 13:03
  • $\begingroup$ @Ben The sampled signal with the delay was roughly the size of the original so the samples were lost beforehand, is it just the way it is or can i do something to retrieve them? $\endgroup$ – EdwardBlackdd Feb 3 '19 at 14:28
  • $\begingroup$ You could use an IIR filter to resample and use the filtfilt function. It will not work for causal applications though such as real-time processing. $\endgroup$ – Ben Feb 3 '19 at 16:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.