103 views (last 30 days)
Show older comments
Liang on 18 Sep 2020
-
-
Link
Direct link to this question
https://support.mathworks.com/matlabcentral/answers/595918-building-low-pass-filter-with-sinc-function
Answered: Preston Pan on 1 Jul 2022
Accepted Answer: Star Strider
Open in MATLAB Online
Dear Community,
I am trying to build a low-pass filter by using a sinc function for my homework assignment. I then use convolution to later filter an audio sample with this filter. However, when I plot the filter in a bode plot it looks like a high-pass filter. Can anyone tell me what I'm doing wrong?
Thanks in advance!
%% Downsampled by K with low-pass filter
% Build filter
clear all; close all
K = 2;
fs = 1600;
N = 51;
n = (-(N-1)/2:1:(N-1)/2);
h = (1/K) * sinc((pi/K)*n);
% Plot frequency response filter
[H, H_vec] = fftFreq(h, fs, 1 );
figure
plot(H_vec*2*pi/fs, abs(H))
filt_tf =tf(h,1,1/fs,'Variable','z^-1');
figure
bode(filt_tf)
function [ X , f ] = fftFreq( data , fs, w )
% Number of FFT points
NFFT = length( data );
% calculate FFT
X = fft(data .* w);
% calculate frequency spacing
df = fs/NFFT;
% calculate unshifted frequency vector
f = (0:(NFFT-1)) * df;
end
1 Comment Show -1 older commentsHide -1 older comments
Show -1 older commentsHide -1 older comments
Bas Zweers on 22 Sep 2020
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/595918-building-low-pass-filter-with-sinc-function#comment_1020202
⋮
You need to use fftshift for the correct frequency plot
Sign in to comment.
Sign in to answer this question.
Accepted Answer
Star Strider on 18 Sep 2020
Open in MATLAB Online
I am not exactly certain what the problem is from a theoretical prespective (I will leave it to you to explore that), however the sinc pulse is too narrow. Increase ‘K’ to 4 or more, and you get a lowpass result.
Also, since this is a discrete filter, the freqz function will do what you want:
figure
freqz(h,1,2^16,fs)
If you are going to use it as a FIR discrete filter, do the actual filtering with the filtfilt function for the best results.
.
4 Comments Show 2 older commentsHide 2 older comments
Show 2 older commentsHide 2 older comments
Liang on 18 Sep 2020
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/595918-building-low-pass-filter-with-sinc-function#comment_1014835
Edited: Liang on 18 Sep 2020
Thanks, it worked for me!
However, I am still figuring out why if K is too small (i.e. 2 or 3) that we apparently get a high-pass filter. My assignment specifically states that we need to design an ideal low-pass filter with a cut-off frequency of pi/2.
So if anyone has some insight please let me know.
Star Strider on 18 Sep 2020
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/595918-building-low-pass-filter-with-sinc-function#comment_1014841
As always, my pleasure!
I have not investigated the sinc function in that respect. Taking its Fourier transform could provide significant insight (analytically if possible, numerically if that is all that is available). The Fourier transform will reveal it to be a square wave (actually rectangle function) in the frequency domain. Mapping the time-domain parameters to the frequency domain parameters to produce the desired design remains the issue.
Liang on 22 Sep 2020
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/595918-building-low-pass-filter-with-sinc-function#comment_1020214
UPDATE:
Apparently, I made a mistake in the mathematical procedure to come up with my sinc low-pass filter. The pi in the sinc function shouldn't be there. Now everything is working correctly. Thanks again for the help.
Star Strider on 23 Sep 2020
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/595918-building-low-pass-filter-with-sinc-function#comment_1020238
As always, my pleasure!
Sign in to comment.
More Answers (1)
Preston Pan on 1 Jul 2022
Open in MATLAB Online
Consider removing the pi in the argument of sinc. I get that scaling is necessary to respect the fourier scaling relationship and preserve unit gain in the passband but I think that would just be rect(K*t) <--> 1/|K| * sinc(f/K).
When I removed it and did
h=(1/K)*sinc(n/K)
the filter produced the desired behavior.
0 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Sign in to answer this question.
See Also
Categories
Signal ProcessingSignal Processing ToolboxSignal Generation and Preprocessing
Find more on Signal Generation and Preprocessing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office