# Using sinc as a filter

I'm trying to use a cardinal sine as a lowpass filter for a cosine signal with a fundamental of 1k.

In frequency domain, what really happens is that I'm multiplying two impulses (centered at -1k and 1k) with a rectangular pulse of width equals 2 (convolution is multiplication in frecuency domain). The result would be a constant zero function. However using the above code in Matlab I'm getting again the sinc function as output. Any help would be appreciated (attached the full plot).

D = 5;
f0 = 1000;
fm = 2*f0;    % nyquist reestriction

t = (-D:1/fm:D);
x = cos(1000*2*pi*t);
h = sinc(t);

x_p = filter(h,1,x);
plot(x_p);


• Using conv(h,x) I'm getting two cardinal sines, also tested with filter(x,1,h)
– JMFS
Apr 30 '14 at 23:36

You messed with f0/fm relationship. Try this code:

D = 5;
f0 = 1e3;
fm = 40e3;
t = 0:1:2*fm*D-1;
x = cos(2*pi*t*f0/fm);
h = sinc(-D*pi:1/10:D*pi);
x_p = filter(h,1,x);
plot(x_p); plot(x); plot(h);
figure; psd(x,2^11,fm);
figure; psd(h,2^11,fm);
figure; psd(x_p,2^11,fm);

• Thanks Dagoff, I run your code but x_p is still returning a cosine, like not filtering operation was made. F0 is the fundamental frequency of the cosine and fm is the sample rate. Wasn't <br> a typo?
– JMFS
May 1 '14 at 15:03
• Yes, because I made f0 in passband of sinc filter. If you want to reject cosine, make for example f0=4e3 in my code and cosine magnitude will go down. You can observe exact frequency response of sinc filter using psd and cosine outside its passband will be rejected. Of course, residual cosine still will be there, because filter rejection is not infinite. May 1 '14 at 16:16
• It took me time to analyze your code, thanks for your patience :D I realize that when using a sampling rate of 10Hz instead of 2000Hz (or higher) in the original code (as you did in yours) I can successfully filter the signal. Could you explain to me why did you choose 10 Hz? Is the filter's length also an important parameter?
– JMFS
May 2 '14 at 19:27
• Probably, you meant 10Hz for f0, not the sampling rate fm. Signal spectrum (f0 in our case) should be less than fm/2. That's what you are missing. May 3 '14 at 0:59
• 1/10 is a normalized frequency. We already chose sampling frequency to be 40KHz. Read this en.wikipedia.org/wiki/… May 7 '14 at 4:21

Probably the problem was happening because I was using a filter as long as my signal, so I was getting only the transient instead of the whole filtered result; as Honglei Chen graciously explained in this thread. He also suggested to me to use this version of the code:

D = 5;
f0 = 1000;
fm = 2*f0;    % nyquist reestriction
t = (-D:1/fm:D-1/fm);
x = cos(1000*2*pi*t);
h = sinc(t(3*numel(t)/8:5*numel(t)/8-1));
x_p = filter(h,1,x);
plot(x_p);