当前位置: 首页 > news >正文

MATLAB FFT 入门到实战:信号分析与频率分解的完整指南

文章目录

    • What Is FFT, Anyway?
    • MATLAB FFT Basics: Step-by-Step Code
    • 3 Common FFT Pitfalls (And How to Fix Them)
      • 1. Forgetting to Scale Magnitude
      • 2. Ignoring Symmetry
    • Advanced Tips to Level Up Your FFT Game
      • Zero-Padding for Smoother Plots
      • Filtering Noisy Signals
    • Real-World Use Case: Motor Vibration Analysis
    • Final Thoughts

Have 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 Code

Let’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:

Fs=100;% Sampling frequency (Hz) — take 100 samples/secondT=1/Fs;% Sampling period (seconds per sample)L=100;% Length of signal (1 second total)t=(0:L-1)*T;% Time vector from 0 to 0.99 seconds

Next, create the signal: two sine waves with amplitudes 2 (5Hz) and3 (20Hz):

signal=2*sin(2*pi*5*t)+3*sin(2*pi*20*t);

Now run FFT:

Y=fft(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:

P2=abs(Y)/L;% Divide by number of samples to scaleP1=P2(1:L/2+1);% 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!):

f=Fs*(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 Magnitude

If 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 Symmetry

Plotting all ofYwill give you a mirrored frequency plot (useless!). Stick to the first half (1:L/2+1).

###3. Wrong Sampling Frequency
IfFsis too low, you getaliasing: high frequencies look like low ones. For example, a30Hz signal with Fs=50Hz will appear as 20Hz (50-30=20). Always setFsto at least twice your highest expected frequency (Nyquist rate).

Advanced Tips to Level Up Your FFT Game

Zero-Padding for Smoother Plots

Zero-padding adds zeros to your signal before FFT. It doesn’t add new info, but it makes your frequency plot smoother (higher resolution):

Y=fft(signal,256);% 256 is longer than original L=100L_new=256;f_new=Fs*(0:(L_new/2))/L_new;P2=abs(Y)/L;% Scale by original L (zeros don't add energy!)P1_new=P2(1:L_new/2+1);P1_new(2:end-1)=2*P1_new(2:end-1);plot(f_new,P1_new);% Smoother peaks!

Filtering Noisy Signals

Suppose your signal has random noise:

signal_noisy=signal+randn(size(t))*0.5;% Add Gaussian noise

FFT tells you which frequencies to keep (5Hz and 20Hz). Use a bandpass filter to remove noise:

order=100;% Higher order = sharper cutofff_pass=[4,21];% Keep 4-21Hzb=fir1(order,f_pass/(Fs/2),'bandpass');% Design filtersignal_filtered=filtfilt(b,1,signal_noisy);% Zero-phase filter (no delay!)

Plotsignal_filtered—noise is gone, original signal is back!

Real-World Use Case: Motor Vibration Analysis

Let’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).

  1. Record vibration data with a sensor.
  2. Import into MATLAB.
  3. Run FFT—check for peaks at 50Hz.
  4. 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 Thoughts

MATLAB FFT is a powerful tool for signal analysis, and it’s easier than you think. Today we covered:

  • Time vs frequency domain
  • Basic FFT code and scaling
  • Common pitfalls (and fixes)
  • Zero-padding and filtering noisy signals
  • Real-world use case

The 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.jsqmd.com/news/588623/

相关文章:

  • 如何高效使用Sketch设计稿转HTML工具:5步实现设计到代码的智能转换
  • Python+AI:自动分析财报数据的5个实战技巧
  • 低成本搭建方案:树莓派运行OpenClaw连接千问3.5-9B云接口
  • GitHub中文界面终极指南:5分钟免费解锁中文GitHub
  • 【顶刊复现】跟网型逆变器小干扰稳定性分析与控制策略优化Matlab代码
  • 过期域名抢注对SEO优化有什么影响
  • 如何降低seo关键字价格
  • 华为ENSP OSPF实验避坑指南:配置Stub区域、路由聚合与DR选举的常见错误
  • 快马平台十分钟实战:用AI生成代码快速原型验证龙虾部署理念
  • 终极NCM音乐解密指南:快速解锁网易云音乐加密文件
  • [数智金融] [3] 关于经济数据分析模块的大致思路
  • 【电池特征提取+SOH估计】基于PINN物理信息神经网络的锂电池SOH估计 Matlab代码(多输入单输出)
  • 计算机毕业设计:Python新能源汽车舆情与个性化推荐平台 Django框架 snowNLP 协同过滤推荐算法 requests爬虫 可视化(建议收藏)✅
  • 【声纳与人工智能融合——从理论前沿到自主系统实战(进阶篇)】第十五章 条件正规化流(CNF)的AUV风险敏感路径规划
  • 2026年无锡口碑好的草坪种子直销厂家推荐,高羊茅种子/紫花苜蓿种子/波斯菊种子/牧草种子/早熟禾种子,草坪种子厂家推荐 - 品牌推荐师
  • LongCat-Image 图像生成模型,编辑能力登顶开源SOTA
  • 智慧树学习助手:如何用3分钟安装实现自动化学习体验
  • 2026 高复购精油榜单:愉禾五行系列精油,用过都回购 - 新闻快传
  • 从零搭建AI开发环境:Python 3.10.11、CUDA 12.1与PyTorch一站式配置指南
  • 窗口管理效率神器:AlwaysOnTop实现多任务无缝切换
  • 告别旧版多协议接入?新版OneNET物模型与OneJSON实战配置详解
  • 郭锐入局智界,再造一个“荣耀”?
  • 苍穹外卖需要注意的地方
  • 人工智能大语言模型和Vibe Coding:Simio与LLM大语言模型辅助的自动化建模
  • YOLOV26 AutoFormBench:自动化表单理解的基准数据集
  • 六安的企业商家为什么要做豆包推荐优化(GEO优化) - 新闻快传
  • 2025届毕业生推荐的六大降AI率方案推荐
  • 5步解锁八大网盘直链下载:告别限速与客户端依赖的终极指南
  • seo网站诊断的步骤是什么
  • “爱希里”不是新锐而是老品牌 - 新闻快传