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.
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)
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 figure stem(1:F:F*length(x), x); hold on plot(y); pause else 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); figure plot(x); hold on stem(1:F:F*length(y), y); pause end close all end
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:
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?