% Signal parameters:
f = [ 440 880 1000 2000 ]; % frequencies
M = 256; % signal length
Fs = 5000; % sampling rate
% Generate a signal by adding up sinusoids:
x = zeros(1,M); % pre-allocate 'accumulator'
n = 0:(M-1); % discrete-time grid
for fk = f;
x = x + sin(2*pi*n*fk/Fs);
end
% Filter parameters:
L = 257; % filter length
fc = 600; % cutoff frequency
% Design the filter using the window method:
hsupp = (-(L-1)/2:(L-1)/2);
hideal = (2*fc/Fs)*sinc(2*fc*hsupp/Fs);
h = hamming(L)' .* hideal; % h is our filter
% Choose the next power of 2 greater than L+M-1
Nfft = 2^(ceil(log2(L+M-1))); % or 2^nextpow2(L+M-1)
% Zero pad the signal and impulse response:
xzp = [ x zeros(1,Nfft-M) ];
hzp = [ h zeros(1,Nfft-L) ];
% Transform the signal and the filter:
X = fft(xzp);
H = fft(hzp);
Y = X .* H;
y = ifft(Y);
relrmserr = norm(imag(y))/norm(y) % check... should be zero
y = real(y);