FFT for mortals

FFT cho người bình thường

Sound is pressure changing over time. Your ear and brain are extraordinarily good at noticing when that pressure repeats at a steady rate — we call that pitch. Machinery rarely produces a single pure tone; it produces stacks of periodic components plus hiss. The Fourier transform is the mathematical tool that answers a deceptively simple question: if I record a few milliseconds of microphone data, which frequencies are present and how loud is each one? Sonarish builds its spectrum analyzer and scrolling spectrogram on that question, running entirely on your phone without sending audio to a server.

The discrete Fourier transform, computed efficiently as the fast Fourier transform or FFT, takes N consecutive audio samples and produces N complex numbers representing sine and cosine contributions at equally spaced frequencies. We rarely display the full complex result. For noise and fault work we care about magnitude — how strong each frequency bin is — usually converted to decibels so quiet and loud components share one readable axis.

From one FFT to a moving picture

A single FFT snapshot tells you what happened in one short slice of time. Real machines and rooms change slowly but not infinitely slowly, so Sonarish uses a short-time Fourier transform: chop the continuous microphone stream into overlapping windows, FFT each window, and plot magnitude against time. Window size trades frequency resolution against time resolution. At forty-eight kilohertz sample rate, a window of two thousand forty-eight samples yields bin width of about eleven point seven hertz — fine enough to separate a hundred twenty hertz mains hum from its second harmonic at two hundred forty. A four thousand ninety-six sample window halves that bin width but reacts more slowly to sudden clicks.

We multiply each window by a Hann function before transforming. That tapering reduces spectral leakage — the smearing of energy into neighboring bins when a tone's frequency falls between bin centers. For tonal machinery with sharp harmonics, Hann is a sensible default. Hop size at fifty percent overlap keeps the scrolling waterfall visually smooth without doubling CPU cost unnecessarily.

Reading what the graph means

A peak at one hundred twenty hertz in a country with sixty hertz mains often marks a two-pole induction motor spinning at three thousand six hundred RPM, because electrical frequency times pole pairs maps to mechanical rotation. Bearing wear introduces sidebands that are not simple multiples of shaft speed — energy appearing at unexpected offsets is sometimes the first audible clue before vibration analysts confirm fault frequencies in a report. Broadband hiss rising across many bins suggests turbulence, brush arcing, or loose panels rattling without a single dominant tone.

Sonarish labels peaks automatically when signal-to-noise ratio exceeds about six decibels above a locally estimated noise floor, but the app does not pretend to diagnose your washing machine. It shows the spectrum honestly and lets experience — or our baseline comparison workflow — interpret change over time.

Performance and threading on a phone

FFT work never runs inside the audio capture callback. That path is reserved for copying samples into a lock-free ring buffer, as described in life on the audio thread. A background queue pulls blocks, applies window and FFT using platform libraries — Accelerate vDSP on iOS, a compact FFT implementation on Android — and hands magnitude arrays to the UI thread. Even when transforms complete faster, we cap spectrum redraws at fifteen to thirty hertz because humans cannot interpret a flickering bar chart at sixty hertz and battery matters in a factory walk-through.

Common pitfalls are worth naming because they confuse beginners and reviewers alike. Aliasing — folding high frequencies into low ones — is largely handled by the phone's audio hardware anti-alias filters, but you still should not point a mic at a whistle near the Nyquist limit and trust the graph blindly. Confusing decibels relative to digital full scale with decibels sound pressure level invalidates any comparison to environmental law; our A-weighting and decibels post explains that distinction. Shrinking window size to make peaks look sharper actually widens bins and merges adjacent harmonics into one fat bar — pretty UI, worse physics.

Phyzix includes a live microphone FFT view for classroom demos where students clap, whistle, and watch bins light up. Wheria does not need spectral analysis, but the same sampling discipline — respect Nyquist, never block real-time capture — runs through every uranashel app that touches a microphone or accelerometer at high rate.

Âm thanh là áp suất thay đổi theo thời gian. Tai và não cực kỳ giỏi nhận ra khi áp suất lặp đều — ta gọi đó là cao độ. Máy móc hiếm khi chỉ phát một tone thuần; chúng tạo chồng thành phần tuần hoàn cộng tiếng rít. Biến đổi Fourier là công cụ toán trả lời câu hỏi đơn giản mà đầy bẫy: nếu ghi vài mili giây dữ liệu micro, tần số nào có mặt và mỗi tần to cỡ nào? Sonarish xây máy phân tích phổ và spectrogram cuộn trên câu hỏi đó, chạy hoàn toàn trên điện thoại, không gửi audio lên server.

Biến đổi Fourier rời rạc, tính hiệu quả bằng FFT, lấy N mẫu audio liên tiếp và cho N số phức biểu diễn thành phần sin và cos ở các tần cách đều. Hiếm khi hiển thị toàn bộ phần phức. Với đo ồn và lỗi máy, ta quan tâm biên độ — mỗi bin mạnh thế nào — thường đổi sang decibel để thành phần nhỏ và lớn cùng một trục đọc được.

Từ một FFT thành hình động

Một ảnh FFT cho biết vài mili giây. Máy và phòng thay đổi chậm nhưng không vô hạn chậm, nên Sonarish dùng biến đổi Fourier thời gian ngắn: cắt luồng micro thành cửa sổ chồng lấn, FFT từng cửa, vẽ biên độ theo thời gian. Kích cỡ cửa sổ đánh đổi độ phân giải tần với thời gian. Ở sample rate 48 kHz, cửa 2048 mẫu cho độ rộng bin khoảng 11,7 Hz — đủ tách hum lưới 120 Hz khỏi hài thứ hai 240 Hz. Cửa 4096 mẫu thu hẹp bin một nửa nhưng phản ứng chậm hơn với click đột ngột.

Mỗi cửa sổ nhân hàm Hann trước khi biến đổi. Độ taper giảm rò phổ — năng lượng tràn sang bin lân cận khi tần tone nằm giữa tâm bin. Với máy tone sắc, Hann là mặc định hợp lý. Hop 50% overlap giữ waterfall cuộn mượt mà không nhân đôi chi phí CPU vô ích.

Đọc đồ thị nghĩa là gì

Đỉnh 120 Hz ở nước lưới 60 Hz thường là motor induction hai cực 3600 vòng/phút, vì tần điện nhân số cực ánh xạ sang quay cơ. Bạc đạn mòn thêm sideband không phải bội số đơn giản của tốc trục — năng lượng ở offset lạ đôi khi là manh mối nghe được trước khi báo cáo rung xác nhận tần lỗi. Hiss broadband tăng trên nhiều bin gợi xoáy, hồ quang chổi, hoặc tấm lỏng rung không có tone chủ đạo.

Sonarish tự gắn nhãn đỉnh khi tỉ số tín hiệu trên nhiễu vượt khoảng 6 dB so nền ước local, nhưng app không giả chẩn đoán máy giặt. Nó hiện phổ thật và để kinh nghiệm — hoặc quy trình so baseline — diễn giải thay đổi theo thời gian.

Hiệu năng và luồng trên điện thoại

FFT không chạy trong callback capture audio. Đường đó dành cho copy mẫu vào ring buffer lock-free, như sống trên audio thread. Queue nền lấy block, cửa sổ và FFT bằng thư viện nền tảng — Accelerate vDSP trên iOS, FFT gọn trên Android — rồi đưa mảng biên độ lên UI. Dù transform nhanh hơn, team giới hạn vẽ phổ 15–30 Hz vì người không đọc được biểu đồ nhấp nháy 60 Hz và pin quan trọng khi đi khảo sát xưởng.

Điểm hay nhầm đáng nêu vì làm rối người mới và reviewer. Alias — gập tần cao xuống thấp — phần lớn do phần cứng chống alias của điện thoại, nhưng vẫn đừng chỉ micro vào còi gần Nyquist rồi tin đồ thị mù quáng. Lẫn dB so với full scale kỹ thuật số với dB áp suất âm vô hiệu mọi so sánh luật môi trường; bài A-weighting và decibel giải thích. Thu nhỏ cửa sổ để đỉnh trông sắc thực ra nới bin và dính các hài liền kề thành một cột béo — UI đẹp, vật lý tệ hơn.

Phyzix có FFT micro live cho lớp học: học sinh vỗ tay, huýt sáo, xem bin sáng. Wheria không cần phân tích phổ, nhưng cùng kỷ luật lấy mẫu — tôn trọng Nyquist, không block capture real-time — chạy xuyên mọi app uranashel chạm micro hay gia tốc tốc cao.