MATLAB FFT 入门到实战:信号分析与频率分解的完整指南
文章目录What Is FFT, Anyway?MATLAB FFT Basics: Step-by-Step Code3 Common FFT Pitfalls (And How to Fix Them)1. Forgetting to Scale Magnitude2. Ignoring SymmetryAdvanced Tips to Level Up Your FFT GameZero-Padding for Smoother PlotsFiltering Noisy SignalsReal-World Use Case: Motor Vibration AnalysisFinal ThoughtsHave you ever stared at a wiggly time-series graph and thought, “What on earth is going on here?” Like, when you record the sound of your guitar string and see a messy wave but want to know exactly which notes (frequencies) are playing? Or when your Arduino’s accelerometer data has random jitters and you need to find the underlying pattern? That’s where MATLAB’s FFT comes in—your superpower to turn chaos into clarity!Today, I’m going to walk you through everything from FFT basics to writing working MATLAB code. I remember my first FFT attempt: I messed up scaling so bad my peaks were 10x higher than they should be (oops!). So I’ll share all those mistakes to save you from the same pain. Let’s dive in!What Is FFT, Anyway?Let’s keep theory light—promise. FFT stands for Fast Fourier Transform. It’s a math trick to convert a signal from thetime domain(what happens when) to thefrequency domain(what frequencies are present).Imagine a bowl of fruit salad: time domain is looking at the whole bowl, mixing apples, bananas, and grapes. Frequency domain is sorting them into separate piles—each pile is a frequency component. FFT does exactly that for your signal: splits overlapping frequencies into individual parts you can see clearly.Why “fast”? The original Fourier Transform is slow for big datasets (O(n²) time). FFT uses clever algorithms to cut it down to O(n log n)—a game-changer for thousands of data points.MATLAB FFT Basics: Step-by-Step CodeLet’s get hands-on with a simple example: a signal with two frequencies (5Hz and20Hz). We’ll generate it, run FFT, and plot the results.First, set your parameters:Fs100;% Sampling frequency (Hz) — take 100 samples/secondT1/Fs;% Sampling period (seconds per sample)L100;% Length of signal (1 second total)t(0:L-1)*T;% Time vector from 0 to 0.99 secondsNext, create the signal: two sine waves with amplitudes 2 (5Hz) and3 (20Hz):signal2*sin(2*pi*5*t)3*sin(2*pi*20*t);Now run FFT:Yfft(signal);But wait—Yis a complex array. To get the magnitude (how strong each frequency is), takeabs(Y). Also, FFT output is symmetric for real signals—so we only need the first half:P2abs(Y)/L;% Divide by number of samples to scaleP1P2(1:L/21);% First half (includes DC and Nyquist)P1(2:end-1)2*P1(2:end-1);% Multiply non-DC/Nyquist by 2 (since they split into two halves)Compute the frequency axis (critical for plotting!):fFs*(0:(L/2))/L;% From 0 to Fs/2 (Nyquist frequency)Finally, plot time and frequency domains:subplot(2,1,1);plot(t,signal);title(Time Domain Signal);xlabel(Time (s));ylabel(Amplitude);subplot(2,1,2);plot(f,P1);title(Frequency Domain);xlabel(Frequency (Hz));ylabel(Amplitude);Run this code—you’ll see two sharp peaks at5Hz and 20Hz, exactly what we put in! That’s FFT magic in action.3 Common FFT Pitfalls (And How to Fix Them)I’ve fallen for all these—don’t repeat my mistakes!1. Forgetting to Scale MagnitudeIf you skip scaling, your peaks will be way off (like 100 instead of 2). Why? Because FFT doesn’t auto-scale. Always divide byL(number of samples) and multiply non-DC/Nyquist by2.2. Ignoring SymmetryPlotting all ofYwill give you a mirrored frequency plot (useless!). Stick to the first half (1:L/21).###3. Wrong Sampling FrequencyIfFsis too low, you getaliasing: high frequencies look like low ones. For example, a30Hz signal with Fs50Hz will appear as 20Hz (50-3020). Always setFsto at least twice your highest expected frequency (Nyquist rate).Advanced Tips to Level Up Your FFT GameZero-Padding for Smoother PlotsZero-padding adds zeros to your signal before FFT. It doesn’t add new info, but it makes your frequency plot smoother (higher resolution):Yfft(signal,256);% 256 is longer than original L100L_new256;f_newFs*(0:(L_new/2))/L_new;P2abs(Y)/L;% Scale by original L (zeros dont add energy!)P1_newP2(1:L_new/21);P1_new(2:end-1)2*P1_new(2:end-1);plot(f_new,P1_new);% Smoother peaks!Filtering Noisy SignalsSuppose your signal has random noise:signal_noisysignalrandn(size(t))*0.5;% Add Gaussian noiseFFT tells you which frequencies to keep (5Hz and 20Hz). Use a bandpass filter to remove noise:order100;% Higher order sharper cutofff_pass[4,21];% Keep 4-21Hzbfir1(order,f_pass/(Fs/2),bandpass);% Design filtersignal_filteredfiltfilt(b,1,signal_noisy);% Zero-phase filter (no delay!)Plotsignal_filtered—noise is gone, original signal is back!Real-World Use Case: Motor Vibration AnalysisLet’s say you’re a mechanical engineer checking a motor. It should vibrate at50Hz. If it’s at 55Hz, something’s loose (like a bolt).Record vibration data with a sensor.Import into MATLAB.Run FFT—check for peaks at 50Hz.If you see a 55Hz peak, inspect the motor’s bolts.That’s how FFT is used in predictive maintenance—saving time and money!Final ThoughtsMATLAB FFT is a powerful tool for signal analysis, and it’s easier than you think. Today we covered:Time vs frequency domainBasic FFT code and scalingCommon pitfalls (and fixes)Zero-padding and filtering noisy signalsReal-world use caseThe best way to learn is to practice! Grab any signal (sound clip, sensor data, or even a sine wave) and run FFT on it. You’ll be surprised at what you find.Happy coding—may your FFT peaks always be sharp!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2483668.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!