Upload
latec
View
63
Download
1
Embed Size (px)
DESCRIPTION
Filtering tutorial matlab
Citation preview
5/21/2018 Simple FFT and Filtering Tutorial With Matlab - CodeProject
10,746,229 members (64,855 online)
home quick answers discussions features community
helpSearch for articles, question
Articles General Programming Algorithms & Recipes Math
Article
Browse Code
Stats
Revisions (7)
Alternatives
Comments (1)
About A
Type
Licence
First Posted
Views
Bookmarked
Related
MATLAB Engin
Sound ScanneAnalyzer
A Beginner's TUnderstandingAttributes in AS
Simple Audio O
and Spectrum
Building COMUsing MATLAB
Homomorphic C#.NET
Solving Engine
Using MATLABLibrary
Visualizing Sou
Programming applications in tutorials
Personal Wave
Image TrackingVision Using FCorrelation
2D FFT of an I
Exocortex.DSP
Image Process
MATLAB Share
Using MatLab Visual Studio 6
AForge.NET oframework
Multimedia Pe
File System FiTutorial
Grasshopper.iics , 20 Jan 2012Rate this:
Simple FFT and Filtering Tutorial with Matlab
Understanding FFT was never soeasy!
IntroductionLet us understand FFT. It is Fast Fourier Transform, an algorithm to calculate DFT or discretefourier transform in fast and efficient way. The first question that arises seeing the title is what thehell a tutorial on FFT doing in the new article section of code project in the year 2012 when thealgorithm is about 50 years old. Great Question. Bythe way, you can refer following links forunderstanding the same in a complicated way.
http://en.wikipedia.org/wiki/Discrete_Fourier_transform
http://en.wikipedia.org/wiki/Fast_Fourier_transform
Background
One of the reasons, I thought I must write this tutorial is because I have seen many engineersfinding it difficult to understand what exactly is a frequency domain, what exactly is filtering. Howcan we filter a signal in simple way. What is this symmetrical features of FFT and moreimportantly understanding the core facts about FFT. So if you are a programming Pro and a killerprogrammer having written your ownDSP logicsand algorithms, thenthis article may not giveyou any new or interesting things. But if you have just pickedsignal processing, you might find itquite helpful (Hopefully).
Using the Code
First Let us construct a simple sinusoidal signal of 50Hz with amplitude=5. I am consideringasampling frequency of 100 times the message frequency(it should it least be twice as parNequistrate), which means I will collect 1000 samples from the actual analog sinusoidal signal.So if the frequency is 50, that means you will get one sample at 1/50 or .02 seconds. If you want10 samples, you have to gather the samples for .2 seconds. You will sample the signal at every1/5000 seconds.
Collapse | Copy CodeCollapse | Copy Code
!"#$%&"#%'("!)*$$%
+("*,'(%-"$.+(.*$,!%/"&)(0123)40)!)-5%
467-2/589:0; 71
4.27 (4 votes)
All-Topics
articles
http://www.codeproject.com/Articles/11896/Programming-DirectShow-applications-in-C-A-set-ofhttp://www.codeproject.com/Articles/4219/Solving-Engineering-Problems-Using-MATLAB-C-Math-Lhttp://www.codeproject.com/Tips/456916/Homomorphic-Filtering-in-Csharp-NEThttp://www.codeproject.com/Articles/577776/Filters-and-Attributes-in-ASPNET-MVChttp://www.codeproject.com/Articles/58148/Sound-Scanner-and-FFT-Analyzerhttp://www.codeproject.com/Articles/4216/MATLAB-Engine-APIhttp://www.codeproject.com/Tags/All-Topicshttp://en.wikipedia.org/wiki/Fast_Fourier_transformmailto:?subject=Simple%20FFT%20and%20Filtering%20Tutorial%20with%20Matlab%20-%20CodeProject&body=http%3a%2f%2fwww.codeproject.com%2fArticles%2f317054%2fSimple-FFT-and-Filtering-Tutorial-with-Matlabhttps://www.facebook.com/sharer/sharer.php?u=http%3a%2f%2fwww.codeproject.com%2fArticles%2f317054%2fSimple-FFT-and-Filtering-Tutorial-with-Matlabhttp://www.linkedin.com/shareArticle?mini=true&url=http%3a%2f%2fwww.codeproject.com%2fArticles%2f317054%2fSimple-FFT-and-Filtering-Tutorial-with-Matlab&title=Simple%20FFT%20and%20Filtering%20Tutorial%20with%20Matlab%20-%20CodeProjecthttp://twitter.com/home?status=Simple%20FFT%20and%20Filtering%20Tutorial%20with%20Matlab%20-%20CodeProject%20http%3a%2f%2fwww.codeproject.com%2fArticles%2f317054%2fSimple-FFT-and-Filtering-Tutorial-with-Matlabhttps://plus.google.com/share?url=Check%20out%20Simple%20FFT%20and%20Filtering%20Tutorial%20with%20Matlab%20-%20CodeProject%20http%3a%2f%2fwww.codeproject.com%2fArticles%2f317054%2fSimple-FFT-and-Filtering-Tutorial-with-Matlabmailto:?subject=Simple%20FFT%20and%20Filtering%20Tutorial%20with%20Matlab%20-%20CodeProject&body=http%3a%2f%2fwww.codeproject.com%2fArticles%2f317054%2fSimple-FFT-and-Filtering-Tutorial-with-Matlabhttps://www.facebook.com/sharer/sharer.php?u=http%3a%2f%2fwww.codeproject.com%2fArticles%2f317054%2fSimple-FFT-and-Filtering-Tutorial-with-Matlabhttp://www.linkedin.com/shareArticle?mini=true&url=http%3a%2f%2fwww.codeproject.com%2fArticles%2f317054%2fSimple-FFT-and-Filtering-Tutorial-with-Matlab&title=Simple%20FFT%20and%20Filtering%20Tutorial%20with%20Matlab%20-%20CodeProjecthttp://twitter.com/home?status=Simple%20FFT%20and%20Filtering%20Tutorial%20with%20Matlab%20-%20CodeProject%20http%3a%2f%2fwww.codeproject.com%2fArticles%2f317054%2fSimple-FFT-and-Filtering-Tutorial-with-Matlabhttps://plus.google.com/share?url=Check%20out%20Simple%20FFT%20and%20Filtering%20Tutorial%20with%20Matlab%20-%20CodeProject%20http%3a%2f%2fwww.codeproject.com%2fArticles%2f317054%2fSimple-FFT-and-Filtering-Tutorial-with-Matlabhttp://www.codeproject.com/script/Bookmarks/Add.aspx?obid=317054&obtid=2&action=AddBookmark&bio=true&bis=mediumhttp://www.codeproject.com/Articles/317054/Simple-FFT-and-Filtering-Tutorial-with-Matlab?display=Printhttp://www.codeproject.com/script/Bookmarks/Add.aspx?obid=317054&obtid=2&action=AddBookmark&bio=true&bis=mediumhttp://www.codeproject.com/Chapters/6/General-Programming.aspxhttp://www.codeproject.com/KB/recipes/http://www.codeproject.com/KB/recipes/#Mathhttp://www.codeproject.com/script/Articles/Latest.aspxhttp://www.codeproject.com/script/Answers/List.aspx?tab=activehttp://www.codeproject.com/http://www.codeproject.com/http://www.codeproject.com/http://www.codeproject.com/http://www.codeproject.com/http://www.codeproject.com/http://www.codeproject.com/http://www.codeproject.com/http://www.codeproject.com/http://www.codeproject.com/http://www.codeproject.com/http://www.codeproject.com/Articles/6236/Building-COM-Components-Using-MATLAB-Part-Ihttp://www.codeproject.com/script/Articles/Latest.aspxhttp://www.codeproject.com/Tags/All-Topicshttps://plus.google.com/share?url=Check%20out%20Simple%20FFT%20and%20Filtering%20Tutorial%20with%20Matlab%20-%20CodeProject%20http%3a%2f%2fwww.codeproject.com%2fArticles%2f317054%2fSimple-FFT-and-Filtering-Tutorial-with-Matlabhttp://twitter.com/home?status=Simple%20FFT%20and%20Filtering%20Tutorial%20with%20Matlab%20-%20CodeProject%20http%3a%2f%2fwww.codeproject.com%2fArticles%2f317054%2fSimple-FFT-and-Filtering-Tutorial-with-Matlabhttp://www.linkedin.com/shareArticle?mini=true&url=http%3a%2f%2fwww.codeproject.com%2fArticles%2f317054%2fSimple-FFT-and-Filtering-Tutorial-with-Matlab&title=Simple%20FFT%20and%20Filtering%20Tutorial%20with%20Matlab%20-%20CodeProjecthttps://www.facebook.com/sharer/sharer.php?u=http%3a%2f%2fwww.codeproject.com%2fArticles%2f317054%2fSimple-FFT-and-Filtering-Tutorial-with-Matlabmailto:?subject=Simple%20FFT%20and%20Filtering%20Tutorial%20with%20Matlab%20-%20CodeProject&body=http%3a%2f%2fwww.codeproject.com%2fArticles%2f317054%2fSimple-FFT-and-Filtering-Tutorial-with-Matlabhttp://www.codeproject.com/Articles/317054/Simple-FFT-and-Filtering-Tutorial-with-Matlab#http://www.codeproject.com/Articles/317054/Simple-FFT-and-Filtering-Tutorial-with-Matlab#http://www.codeproject.com/Articles/317054/Simple-FFT-and-Filtering-Tutorial-with-Matlab#http://www.codeproject.com/Articles/317054/Simple-FFT-and-Filtering-Tutorial-with-Matlab#http://www.codeproject.com/Articles/317054/Simple-FFT-and-Filtering-Tutorial-with-Matlab#http://www.codeproject.com/Articles/317054/Simple-FFT-and-Filtering-Tutorial-with-Matlab#http://www.codeproject.com/Articles/317054/Simple-FFT-and-Filtering-Tutorial-with-Matlab#http://www.codeproject.com/Articles/317054/Simple-FFT-and-Filtering-Tutorial-with-Matlab#http://www.codeproject.com/Articles/317054/Simple-FFT-and-Filtering-Tutorial-with-Matlab#http://www.codeproject.com/Articles/317054/Simple-FFT-and-Filtering-Tutorial-with-Matlab#http://www.codeproject.com/Articles/317054/Simple-FFT-and-Filtering-Tutorial-with-Matlab#http://www.codeproject.com/Articles/317054/Simple-FFT-and-Filtering-Tutorial-with-Matlab#http://en.wikipedia.org/wiki/Fast_Fourier_transformhttp://en.wikipedia.org/wiki/Discrete_Fourier_transformhttp://www.codeproject.com/script/Membership/View.aspx?mid=8114613http://www.codeproject.com/Articles/317054/Simple-FFT-and-Filtering-Tutorial-with-Matlab?display=Printhttp://www.codeproject.com/script/Bookmarks/Add.aspx?obid=317054&obtid=2&action=AddBookmark&bio=true&bis=mediumhttp://www.codeproject.com/Articles/20869/D-Fast-Wavelet-Transform-Library-for-Image-Proceshttp://www.codeproject.com/Articles/43586/File-System-Filter-Driver-Tutorialhttp://www.codeproject.com/Articles/26357/Multimedia-PeakMeter-Controlhttp://www.codeproject.com/Articles/16859/AForge-NET-open-source-frameworkhttp://www.codeproject.com/Articles/4205/Using-MatLab-Add-in-for-MS-Visual-Studiohttp://www.codeproject.com/Articles/5739/MATLAB-Shared-Libraryhttp://www.codeproject.com/Articles/9727/Image-Processing-Lab-in-Chttp://www.codeproject.com/Articles/2198/Exocortex-DSPhttp://www.codeproject.com/Articles/44166/D-FFT-of-an-Image-in-Chttp://www.codeproject.com/Articles/479975/Image-Tracking-and-Computer-Vision-Using-Fourier-Ihttp://www.codeproject.com/Articles/65207/Personal-Wave-Recorderhttp://www.codeproject.com/Articles/11896/Programming-DirectShow-applications-in-C-A-set-ofhttp://www.codeproject.com/Articles/488655/Visualizing-Soundhttp://www.codeproject.com/Articles/4219/Solving-Engineering-Problems-Using-MATLAB-C-Math-Lhttp://www.codeproject.com/Tips/456916/Homomorphic-Filtering-in-Csharp-NEThttp://www.codeproject.com/Articles/6236/Building-COM-Components-Using-MATLAB-Part-Ihttp://www.codeproject.com/Articles/14873/Simple-Audio-Out-Oscilloscope-and-Spectrum-Analyzehttp://www.codeproject.com/Articles/577776/Filters-and-Attributes-in-ASPNET-MVChttp://www.codeproject.com/Articles/58148/Sound-Scanner-and-FFT-Analyzerhttp://www.codeproject.com/Articles/4216/MATLAB-Engine-APIhttp://www.codeproject.com/Articles/317054/WebControls/#_commentshttp://www.codeproject.com/script/Articles/ListAlternatives.aspx?aid=317054http://www.codeproject.com/script/Articles/ListVersions.aspx?aid=317054http://www.codeproject.com/script/Articles/Statistics.aspx?aid=317054http://www.codeproject.com/script/Articles/ViewDownloads.aspx?aid=317054http://www.codeproject.com/KB/recipes/#Mathhttp://www.codeproject.com/KB/recipes/http://www.codeproject.com/Chapters/6/General-Programming.aspxhttp://www.codeproject.com/script/Content/SiteMap.aspxhttp://www.codeproject.com/KB/FAQs/http://www.codeproject.com/Lounge.aspxhttp://www.codeproject.com/Feature/http://www.codeproject.com/script/Forums/List.aspxhttp://www.codeproject.com/script/Answers/List.aspx?tab=activehttp://www.codeproject.com/http://www.codeproject.com/5/21/2018 Simple FFT and Filtering Tutorial With Matlab - CodeProject
as aveLibrary for Ima
Figure 1: Sinusoidal Signal of frequency 50Hz. Plot of Variable x.
So the figure shows 10 cycles of sine wave of 50 Hz. You can change the plot to stem type tosee the samples.
So lets take FFT.
Collapse | Copy Code
Collapse | Copy Code
'"!!-2/5%
You must have studied about N point FFT. If you do not specify N, then by default N is length ofmessage signal. In this case it is 1001.
Very important thing: FFT divides your Sampling frequency into N equal parts and returns thestrength of the signal at each of these frequency levels. What it means is you are dividing
frequencies from 0 to 5000 into 1001 equal parts. So first point in fft is 5Hz, next represents 10Hz and so on. As the actual frequency of your message is 50 Hz, you must get highest peak atthat level. So let us plot FFT.
Collapse | Copy CodeCollapse | Copy Code
467-2:=>62'5589:0; 71
http://www.codeproject.com/Articles/20869/D-Fast-Wavelet-Transform-Library-for-Image-Proceshttp://www.codeproject.com/Articles/317054/Simple-FFT-and-Filtering-Tutorial-with-Matlab#http://www.codeproject.com/Articles/317054/Simple-FFT-and-Filtering-Tutorial-with-Matlab#http://www.codeproject.com/Articles/317054/Simple-FFT-and-Filtering-Tutorial-with-Matlab#http://www.codeproject.com/Articles/317054/Simple-FFT-and-Filtering-Tutorial-with-Matlab#http://www.codeproject.com/Articles/20869/D-Fast-Wavelet-Transform-Library-for-Image-Proces5/21/2018 Simple FFT and Filtering Tutorial With Matlab - CodeProject
Figure 2: Plot of real part of the FFT output of signal x with highest peak marked. Plot of real part ofVariable F.
What you see? The highest value is located at 11th position, which is nothing but 10th sample asmatlab starts from 1.
10*5?=50Hz
It is so simple. But wait why do we get the imaginary part also? There is absolutely nothingcalled Imaginary values, they reflect past values.
Your signal is Sinusoidal, so you get negative cycle also right. Check this.
Collapse | Copy CodeCollapse | Copy Code
467-2:=>620?>92'55589:0; 71
Figure 3: Plot of imaginary part of the FFT output of signal x with highest peak marked. Plot of imaginarypart Variable F.
what you see?
Yes the same thing with one negative amplitude peak also. I hope you get the idea.
Now let us understand how you can have some filtering with FFT. What I will do is mix couple of
more signal with different frequency with same amplitude and same number of samples withyour signal x.
Collapse | Copy CodeCollapse | Copy Code
/*"&)(0123)40)2!@#$5)-5%
/3"&)(0123)40)2!@3#$5)-5%/"/@/*@/3%
http://www.codeproject.com/Articles/317054/Simple-FFT-and-Filtering-Tutorial-with-Matlab#http://www.codeproject.com/Articles/317054/Simple-FFT-and-Filtering-Tutorial-with-Matlab#http://www.codeproject.com/Articles/317054/Simple-FFT-and-Filtering-Tutorial-with-Matlab#http://www.codeproject.com/Articles/317054/Simple-FFT-and-Filtering-Tutorial-with-Matlab#5/21/2018 Simple FFT and Filtering Tutorial With Matlab - CodeProject
Figure 4: Plot of hybrid signal x containing 50Hz,100Hz,300Hz frequency components. Plot of Variable x.
Our signal now contains three components, a 50Hz,100Hz and 300Hz.
Its fft plot is the proof. Three frequencies at three levels, exactly at 50,100 and 300Hz.
Figure 5: Plot of real part of the FFT output of Hybrid signal x.
I want to recover my original signal. So if I keep the sample at 11th sample and leave the rest
and then take ifft, I must get back my signal right. But the values of both 10th and 11th samplemust be kept. It is called windowing.
5/21/2018 Simple FFT and Filtering Tutorial With Matlab - CodeProject
Figure 6: Desired frequency band marked as a rectangular window in FFT plot of hybrid signal x .
See the red color window I have put over the FFT response of our Hybrid signal.
Collapse | Copy Code
Collapse | Copy Code
'3"A=:7(26=19-B2'58*5%'32*$.**5"'2*$.**5%/:"0!!-2'35%
467-2:=>62/:5589:0; 71
http://www.codeproject.com/Articles/317054/Simple-FFT-and-Filtering-Tutorial-with-Matlab#http://www.codeproject.com/Articles/317054/Simple-FFT-and-Filtering-Tutorial-with-Matlab#5/21/2018 Simple FFT and Filtering Tutorial With Matlab - CodeProject
Grasshopper.iicsCEO Integrated IdeasIndia
gasshopper.iics is a group of like minded programmers and learners in codeproject. The basicobjective is to keep in touch and be notified while a member contributes an article, to check outwith technology and share what we know. We are the "students" of codeproject.This group is managed by Rupam Das, an active author here. Other Notable members includeRanjan who extends his helping hands to invaluable number of authors in their articles andwrites some great articles himself.Rupam Das is mentor of Grasshopper Network,founder and CEO of Integrated IdeasConsultancy Services, a research consultancy firm in India. He has been part of projects inseveral technolo ies includin Matlab C# Android O enCV Dru al Omnet++ le ac C vb
Figure 7: Recovery of Original signal of Frequency component 50Hz from hybrid signal shown in figure4, using rectangular window.
What you see? It is your original signal. But with less amplitude. This is because you choose arectangular window and the loss is due to negative filter gain. You can now read a bit of digitalfilters and apply that.
Here is the complete code which may not be needed, but still I am posting it, in case you decideto spend some time and play with it.
Collapse | Copy CodeCollapse | Copy Code
C6=>: >66%
C6C%C67(= >66!"#$%
&"#%'("!)*$$%+("*,'(%-"$.+(.*$,!%
/"&)(0123)40)!)-5%/*"&)(0123)40)2!@#$5)-5%/3"&)(0123)40)2!@3#$5)-5%/"/@/*@/3%
467-2/5'"!!-2/5%!09D:=E"'(,6=19-B2'5%
F>/0("2*.E.E)26=19-B2/5,3G*55%467-2F>/0(8:=>62'2*.6=19-B2'5,3555
Points of Interest
You can learn Matlab fundamentals from this source
To know the details about any Matlab command, you can simply click on that command in theeditor and press F1. Matlab help file explains the usage and other details about the commandslike fft,sin and so on.
Behind all that complicated mathematics, there is a simple logic. I love that simple logic really.
License
This article, along with any associated source code and files, is licensed under The Code ProjectOpen License (CPOL)
About the Author
http://www.codeproject.com/info/cpol10.aspxhttp://www.grasshoppernetwork.com/Technical/Share/?q=node/104http://www.codeproject.com/Articles/317054/Simple-FFT-and-Filtering-Tutorial-with-Matlab#http://www.codeproject.com/Articles/317054/Simple-FFT-and-Filtering-Tutorial-with-Matlab#http://www.codeproject.com/Members/Grasshopper20115/21/2018 Simple FFT and Filtering Tutorial With Matlab - CodeProject
several technolo ies includin Matlab C# Android O enCV Dru al Omnet le ac C vb
Permalink|Advertise | Privacy| Mobileeb02 | 2.8.140718.1 | Last Updated 20 Jan 2012
Article Copyright 2012 byEverything else Copyright CodePr
Layout: fixed| fluid
gcc, NS-2, Arduino, Raspberry-PI. Off late he has made peace with the fact that he loves C#more than anything else but is still struck in legacy style of coding.Rupam loves algorithm and prefers Image processing, Artificial Intelligence and Bio-medicalEngineering over other technologies.
He is frustrated with his poor writing and "grammer" skills but happy that coding polishes thesefrustrations.
Group type: Organisation
54 members
Follow on Twitter Google
Search this forum Go
Article Top
Comments and Discussions
You must Sign Into use this message board.
Profile popups Spacing Relaxed Noise Medium Layout Normal Per page
50 Update
First Prev Next
Member 10802102 7-May-14 7:55
Last Visit: 31-Dec-99 18:00 Last Update: 21-Jul-14 10:15 Refresh 1
General News Suggestion Question Bug Answer Joke Rant
Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.
Abs() instead of real().
http://www.codeproject.com/Articles/317054/Simple-FFT-and-Filtering-Tutorial-with-Matlab#http://www.codeproject.com/Messages/4815586/Abs-instead-of-real.aspxhttp://www.codeproject.com/Articles/317054/Simple-FFT-and-Filtering-Tutorial-with-Matlab?fid=1679908&df=90&mpp=50&noise=3&prof=True&sort=Position&view=Normal&spc=Relaxedhttp://www.codeproject.com/script/Membership/View.aspx?mid=10802102https://www.codeproject.com/script/Membership/LogOn.aspx?rp=%2fArticles%2f317054%2fSimple-FFT-and-Filtering-Tutorial-with-Matlab%3ffid%3d1679908%26df%3d90%26mpp%3d50%26noise%3d3%26prof%3dTrue%26sort%3dPosition%26view%3dNormal%26spc%3dRelaxedhttp://www.codeproject.com/Articles/317054/Simple-FFT-and-Filtering-Tutorial-with-Matlab#_articleTophttps://plus.google.com/httpsplusgooglecomu0110715467386766077775?rel=authorhttps://plus.google.com/httpsplusgooglecomu0110715467386766077775http://www.twitter.com/GrasshopperNw?rel=authorhttp://www.twitter.com/GrasshopperNwhttp://www.codeproject.com/script/Membership/ListGroupMembers.aspx?mgid=8114613http://www.codeproject.com/Articles/317054/Simple-FFT-and-Filtering-Tutorial-with-Matlab?PageFlow=Fluidhttp://www.codeproject.com/Articles/317054/Simple-FFT-and-Filtering-Tutorial-with-Matlab?PageFlow=FixedWidthmailto:[email protected]://www.codeproject.com/Articles/317054/Simple-FFT-and-Filtering-Tutorial-with-Matlab?display=Mobilehttp://www.codeproject.com/info/privacy.aspxhttp://developermedia.com/http://www.codeproject.com/Articles/317054/Simple-FFT-and-Filtering-Tutorial-with-Matlab