# How to convert filter into frequency domain to do filtering?

I have this filter:

filter_2 = firceqrip(2,0.6,[0.05 0.03]);


I want to convert it to the frequency domain to multiply it by a signal (i.e filter in the frequency domain)

I have a signal and I have a filter.

I converted the signal into frequency domain and now I need to filter it with my filter.

Converting the signal into frequency domain is easy, but how do I filter the signal now?

This is my signal:

freqs = [0.08, 0.2, 0.32, 0.4];
periods = 1./ freqs;
t_max = 4 * periods(1);
t = linspace(0, t_max, 50);

signal = sin(2*pi*0.08*t) + sin(2*pi*0.2*t) + sin(2*pi*0.32*t) + sin(2*pi*0.4*t);

% to find the fft of the signal
N = 64;
signal_spect = abs (fft(signal,N));
signal_spect = fftshift(signal_spect);
F = [-N/2:N/2-1]/N;


now how do I filter the previous signal with my filter? I tried doing this fft(filter_2) but it didn't work out, it's probably a wrong approach, but I do not know what else to do

any help is appreicated

Your filter is an FIR filter, therefore its coefficients are simply the impulse response $h[n]$. Your signal $x[n]$ can be filtered in time domain by convolving with impulse response: $$y[n]=x[n]\star h[n]$$ Knowing that convolution in time domain is equal to multiplication in frequency domain $$Y[f]=X[f]H[f]$$ you only need to calculate two DFT's.

One thing to remember - output will be of the same length as input signal.

Speaking Matlab'ish:

filter_2 = firceqrip(2,0.6,[0.05 0.03]);

freqs = [0.08, 0.2, 0.32, 0.4];
periods = 1./ freqs;
t_max = 4 * periods(1);
t = linspace(0, t_max, 50);

signal = sin(2*pi*0.08*t) + sin(2*pi*0.2*t) + sin(2*pi*0.32*t) + sin(2*pi*0.4*t);

%% NEW CODE HERE %%
% Output is same length as input signal
M = length(signal);
% Find next power of two for sake of calculations
M2 = 2^nextpow2(M);
% Calculate the FFT's
Hf = fft(filter_2, M2);
Xf = fft(signal, M2);
% Multiply in frequency domain
Yf = Hf .* Xf;
% Perform the IFFT
y = ifft(Yf);
% Take only M samples
y = y(1:M);
% Remove this nasty imaginary parts
% comming from round-off errors
y_r = real(y);

% Call the in-built function for sake of comparison
y_l = filter(filter_2, 1, signal);

% Plot the original signal
subplot(311)
plot(signal)
grid on
title('Original signal')

% Plot the results
subplot(312)
plot(y_l);
hold on
plot(y_r, 'r--')
grid on
legend({'in-build function', 'user function'})
title('Filtered signal')

% Plot the relative error
subplot(313)
plot(abs(y_l - y_r))
grid on
title('Relative error')


Comparison of results below. As you can see, it matches perfectly MATLAB filter function.