# Downsampling and Upsampling Using FFTW

I'm having 2 signals which have different sampling rates i.e., 1ms(A) and 4ms(B) and I've tried to upsample/downsample either of the signals based on the code snippet Resampling based on FFT which I believe is equivalent of what is descriped in How to resample audio using fft or dft

% FFTRESAMPLE Resample a real signal by the ratio p/q
function y = fftResample(x,p,q)

% --- take FFT of signal ---
f = fft(x);

% --- resize in the FFT domain ---
% add/remove the highest frequency components such that len(f) = len2
len1 = length(f);
len2 = round(len1*p/q);
lby2 = 1+len1/2;
if len2 < len1
% remove some high frequency samples
d = len1-len2;
f = f(:);
f(floor(lby2-(d-1)/2:lby2+(d-1)/2)) = [];
elseif len2 > len1
% add some high frequency zero samples
d = len2-len1;
lby2 = floor(lby2);
f = f(:);
f = [f(1:lby2); zeros(d,1); f(lby2+1:end)];
end

% --- take the IFFT ---
% odd number of sample removal/addition may make the FFT of a real signal
% asymmetric and artificially introduce imaginary components - we take the
% real value of the IFFT to remove these, at the cost of not being able to
% reample complex signals
y = real(ifft(f));


i.e., reimplemented the fftResample in C++ using FFTW libraries.

The problem is that the output is distorted because of the zero-padding. I'm relatively new to Singal processing and I would appreciate if users can help me understand:

1. if I'm using the right code snippet to upsample/downsample?

2. highlight the benefits of upsampling(A to 4ms) compared to downsampling(B to 1ms)?

The problem is that the output is distorted because

Your method is not quite correct. You have good reference in your question - How to resample audio using fft or dft. Please, read comment of Bjorn Roche to this question. And read answer of Jim Clay. Especially point 2).

Usually it is bad idea to use frequency filtration in frequency domain. There is good blog of Bjorg. See - Why EQ Is Done In the Time Domain

When you downsample a signal you must make sure that the Nyquist condition remains satisfied.

For example, say that signal $x$ has frequencies between $0-25$ Hz, and that the sampling frequency of $x$ is $100$ Hz. The Nyquist frequency is $100/2 = 50$ Hz, thus you could downsample $x$ by a factor of two with no problems.

Now, say that you would like to downsample $x$ by a factor of $4$ instead of $2$. This would mean that the new sampling frequency would be $25$ Hz. Since the new sampling frequency is $25$ Hz, it must not contain frequencies above $25/2=12.5$ Hz. However, $x$ had frequencies between $0-25$ so there would be aliasing. In order to avoid that, you must low-pass filter $x$ first between $0-12.5$ Hz.

The process of low-pass filtering + downsampling is called decimation. You can find more information about it on Wikipedia.

Upsampling would be basicly interpolation, so I would avoid that where possible. I did not look at the URLs you provided, please phrase your problem in full here.