Skip to content

Latest commit

ย 

History

30 Commits

Folders and files

NameName
Last commit message
Last commit date
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐ŸŽต Audio Spectrum Analyzer

Real-Time FFT-Based Audio Visualization on STM32F411

License Platform Build

A real-time audio spectrum analyzer that visualizes sound frequencies on an OLED display. Built with STM32F411, INMP441 MEMS microphone, and SSD1306 display.


โœจ Features

  • Real-Time FFT Processing using ARM CMSIS-DSP (512-point)
  • 8-Band Frequency Visualization with logarithmic scaling
  • Peak Hold Effect for smooth, professional-looking bars
  • DMA-Driven Audio Capture for zero CPU overhead
  • 30+ FPS Display Update rate
  • Low Latency (~40ms total pipeline)

๐Ÿ“ธ Demo

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  OLED Display (128x64)             โ”‚
โ”‚                                    โ”‚
โ”‚  โ–ˆโ–ˆโ–ˆโ–ˆ                              โ”‚
โ”‚  โ–ˆโ–ˆโ–ˆโ–ˆ  โ–ˆโ–ˆโ–ˆโ–ˆ  โ–ˆโ–ˆโ–ˆโ–ˆ                  โ”‚
โ”‚  โ–ˆโ–ˆโ–ˆโ–ˆ  โ–ˆโ–ˆโ–ˆโ–ˆ  โ–ˆโ–ˆโ–ˆโ–ˆ  โ–ˆโ–ˆโ–ˆโ–ˆ            โ”‚
โ”‚  โ–ˆโ–ˆโ–ˆโ–ˆ  โ–ˆโ–ˆโ–ˆโ–ˆ  โ–ˆโ–ˆโ–ˆโ–ˆ  โ–ˆโ–ˆโ–ˆโ–ˆ  โ–ˆโ–ˆโ–ˆโ–ˆ      โ”‚
โ”‚  โ–ˆโ–ˆโ–ˆโ–ˆ  โ–ˆโ–ˆโ–ˆโ–ˆ  โ–ˆโ–ˆโ–ˆโ–ˆ  โ–ˆโ–ˆโ–ˆโ–ˆ  โ–ˆโ–ˆโ–ˆโ–ˆ  โ–ˆโ–ˆโ–ˆโ–ˆโ”‚
โ”‚  โ”€โ”€โ”€โ”€  โ”€โ”€โ”€โ”€  โ”€โ”€โ”€โ”€  โ”€โ”€โ”€โ”€  โ”€โ”€โ”€โ”€  โ”€โ”€โ”€โ”€โ”‚
โ”‚   0     1     2     3     4     5  โ”‚
โ”‚ Bass   Mid   Treble                โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

(Add actual photo/GIF of your working project here)


๐Ÿ› ๏ธ Hardware Requirements

Component Model Quantity Price (โ‚น)
Microcontroller STM32F411 (Black Pill) 1 450
MEMS Microphone INMP441 1 180
OLED Display SSD1306 (128x64, SPI) 1 350
Breadboard Standard 1 50
Jumper Wires M-M, M-F 20 30
Total โ‚น1,060

Optional (Recommended):

  • Logic Analyzer (~โ‚น800) - for I2S debugging
  • Oscilloscope access (university lab)

๐Ÿ“ Wiring Diagram

INMP441 โ†’ STM32F411

INMP441          STM32F411
VDD     โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บ 3.3V
GND     โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บ GND
WS      โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บ PA4  (I2S2_WS)
SCK     โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บ PB13 (I2S2_CK)  โ† NOT PB10 (conflict with I2C2)
SD      โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บ PC3  (I2S2_SD)
L/R     โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บ GND  (Left channel)

SSD1306 โ†’ STM32F411 (I2C2)

SSD1306          STM32F411
VCC     โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บ 3.3V
GND     โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บ GND
SCL     โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บ PB10 (I2C2_SCL)
SDA     โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บ PB3  (I2C2_SDA)

๐Ÿš€ Quick Start

1. Clone or Download

git clone https://github.com/yourusername/audio-spectrum-analyzer.git
cd audio-spectrum-analyzer

2. Open in STM32CubeIDE

  1. File โ†’ Open Projects from File System
  2. Select the project folder
  3. Click Finish

3. Configure STM32CubeMX

Open the .ioc file and verify:

I2S2 Settings:

  • Mode: Master Receive
  • Standard: Philips I2S
  • Data Format: 24 bits
  • Sample Rate: 44.1 kHz
  • DMA: Circular mode, High priority

I2C2 Settings:

  • Speed Mode: Fast Mode (400 kHz)
  • Clock Speed: 400000
  • Addressing Mode: 7-bit

4. Enable CMSIS-DSP

Critical step - don't skip!

  1. Right-click project โ†’ Properties
  2. C/C++ Build โ†’ Settings โ†’ MCU Settings
  3. Check "Use float with printf"
  4. Project โ†’ Manage Middleware โ†’ CMSIS โ†’ DSP โœ…
  5. Add defines: ARM_MATH_CM4, __FPU_PRESENT=1

5. Build & Flash

# In STM32CubeIDE
Project โ†’ Build All (Ctrl+B)
Run โ†’ Debug (F11)

6. Test!

  • Power on STM32
  • Play music near the microphone
  • Watch the spectrum bars dance! ๐ŸŽถ

๐Ÿ“‚ Project Structure

Sound_analyzer/
โ”œโ”€โ”€ README.md                โ† You are here
โ”œโ”€โ”€ README_SETUP.md          โ† Detailed setup guide
โ”œโ”€โ”€ LICENSE                  โ† MIT License
โ”œโ”€โ”€ .gitignore               โ† Git ignore rules
โ”‚
โ”œโ”€โ”€ MIC/                     โ† I2S microphone driver
โ”‚   โ”œโ”€โ”€ i2s_mic.h
โ”‚   โ””โ”€โ”€ i2s_mic.c
โ”‚
โ”œโ”€โ”€ OLED/                    โ† SSD1306 display driver (I2C)
โ”‚   โ”œโ”€โ”€ ssd1306.h / .c
โ”‚   โ”œโ”€โ”€ ssd1306_fonts.h / .c
โ”‚   โ”œโ”€โ”€ ssd1306_conf.h       โ† Active config (I2C2)
โ”‚   โ””โ”€โ”€ ssd1306_conf_i2c.h   โ† I2C config backup
โ”‚
โ”œโ”€โ”€ FFT/                     โ† FFT processing (CMSIS-DSP)
โ”‚   โ”œโ”€โ”€ fft_processor.h / .c โ† CMSIS-DSP version (~1.5 ms)
โ”‚   โ””โ”€โ”€ fft_processor_simple.h / .c  โ† Fallback (~15 ms)
โ”‚
โ”œโ”€โ”€ SPECTRUM/                โ† Frequency band analyzer
โ”‚   โ”œโ”€โ”€ spectrum.h
โ”‚   โ””โ”€โ”€ spectrum.c
โ”‚
โ”œโ”€โ”€ DISPLAY/                 โ† Spectrum visualization
โ”‚   โ”œโ”€โ”€ spectrum_display.h
โ”‚   โ””โ”€โ”€ spectrum_display.c
โ”‚
โ””โ”€โ”€ MAIN_EXAMPLE/            โ† Integration examples
    โ”œโ”€โ”€ main_integration_example.c  โ† Primary (I2C2 OLED)
    โ””โ”€โ”€ main_i2c_version.c          โ† Full self-contained version

๐Ÿง  How It Works

Signal Flow

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  INMP441    โ”‚  I2S Digital Microphone
โ”‚  24-bit     โ”‚  44.1 kHz sampling
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”˜
       โ”‚ I2S DMA (512 samples)
       โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ DMA Buffer  โ”‚  Circular buffer, double-buffering
โ”‚ 512 samples โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”˜
       โ”‚ Half/Full transfer interrupts
       โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ FFT (CMSIS) โ”‚  512-point FFT with Hanning window
โ”‚ ~1.5ms      โ”‚  Magnitude spectrum (256 bins)
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”˜
       โ”‚ Frequency domain data
       โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  Frequency  โ”‚  Map 256 bins โ†’ 8 logarithmic bands
โ”‚  Mapping    โ”‚  Bass, Mid, Treble separation
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”˜
       โ”‚ Bar heights[8]
       โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  Display    โ”‚  Draw bars on SSD1306 via SPI
โ”‚  30+ FPS    โ”‚  Peak hold + smoothing effects
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Frequency Band Mapping

Bar Freq Range Description
0 86-172 Hz Sub-bass (kick drum)
1 258-431 Hz Bass guitar, bass notes
2 517-861 Hz Low midrange (male vocals)
3 947-1550 Hz Midrange (vocals, guitar)
4 1636-2584 Hz Upper midrange (female vocals)
5 2670-4307 Hz Presence (clarity, definition)
6 4393-6890 Hz Brilliance (cymbals, hi-hats)
7 6977-10336 Hz Air (sparkle, ambience)

โš™๏ธ Configuration Options

Adjust Sensitivity

Edit SPECTRUM/spectrum.c:

#define SCALE_FACTOR    3.5f    // Increase for taller bars (try 2.0-6.0)
#define MIN_THRESHOLD   0.01f   // Lower for more sensitivity

Change Smoothing

#define SMOOTHING_FACTOR    0.7f    // 0.0 = no smooth, 0.9 = very smooth
#define PEAK_DECAY_RATE     0.95f   // How fast peaks fall (0.9-0.99)

Customize Display Style

In main loop:

// Simple bars
DISPLAY_DrawBars(bar_heights);

// Bars with peak hold (recommended)
DISPLAY_DrawBarsWithPeaks(bar_heights, peaks);

// Custom style
DISPLAY_DrawSpectrum(bars, peaks, STYLE_OUTLINED_BARS);

๐Ÿ“Š Performance Metrics

Metric Value
Frame Rate 30+ FPS
FFT Processing Time ~1.5 ms
Display Update Time ~15 ms
Total Latency ~40 ms
CPU Usage ~60% @ 100 MHz
Memory (RAM) ~12 KB
Flash Usage ~45 KB

๐Ÿ› Troubleshooting

Display shows nothing

  • Check SPI wiring (SCK, MOSI, CS, DC, RST)
  • Verify ssd1306_conf.h pin definitions match your wiring
  • Test with ssd1306_TestAll() function

"Undefined reference to arm_cfft_f32"

  • Enable CMSIS-DSP library in project settings
  • Add ARM_MATH_CM4 and __FPU_PRESENT=1 defines
  • Rebuild project (clean first)

All bars at zero

  • Check I2S wiring (WS, SCK, SD)
  • Verify microphone VDD = 3.3V (multimeter)
  • Check DMA is in circular mode
  • Print mic_data_ready flag to verify data arrival

Bars jumping erratically

  • Increase SMOOTHING_FACTOR (try 0.8)
  • Adjust SCALE_FACTOR (try 2.0-5.0)
  • Check for EMI/noise near microphone

See README_SETUP.md for detailed troubleshooting.


๐ŸŽ“ Technical Deep Dive

Why Hanning Window?

Without windowing, rectangular truncation of the signal causes spectral leakage - energy from one frequency bin spills into adjacent bins, creating false peaks.

The Hanning window smoothly tapers the signal to zero at edges:

w[n] = 0.5 * (1 - cos(2ฯ€n/N))

This reduces sidelobes by ~31 dB, giving cleaner frequency separation.

Why Logarithmic Band Grouping?

Human hearing is logarithmic - we perceive octaves (doubling of frequency) as equal intervals. Mapping FFT bins logarithmically creates a more musically-relevant display.

Linear spacing would waste resolution on high frequencies we barely hear.

DMA Double Buffering

The 512-sample buffer is split into two halves:

  • Half-transfer interrupt: Process samples 0-255 while DMA fills 256-511
  • Full-transfer interrupt: Process samples 256-511 while DMA wraps to 0-255

This zero-copy design prevents audio dropouts and maximizes throughput.


๐Ÿ“š Resources & References

Documentation

Learning Resources


๐Ÿ”ฎ Future Enhancements

Potential features to add:

  • Beat Detection - Flash on bass hits
  • Bluetooth Audio Input - Analyze phone music
  • RGB LED Strip - WS2812B color visualization
  • BPM Counter - Detect tempo from bass frequencies
  • Multiple Display Modes - Waveform, waterfall, VU meter
  • Auto-Gain Control - Adapt to loud/quiet environments
  • PC Companion App - Python GUI for tuning parameters

๐Ÿค Contributing

Pull requests welcome! Areas for improvement:

  1. Optimize FFT for lower latency
  2. Add alternative windowing functions (Hamming, Blackman)
  3. Implement adaptive frequency ranges based on content
  4. Create PCB design for permanent installation
  5. Port to other STM32 families (F0, F7, H7)

๐Ÿ“„ License

MIT License - see LICENSE file for details.


๐Ÿ‘ค Author

Kushal Pitaliya


๐Ÿ™ Acknowledgments

  • ARM CMSIS-DSP team for optimized FFT algorithms
  • afiskon for SSD1306 display library
  • STMicroelectronics for HAL drivers
  • InvenSense for INMP441 microphone design
  • 3Blue1Brown for Fourier transform visualization

๐Ÿ“ˆ Project Stats

  • Lines of Code: ~1,500
  • Development Time: 8 weeks
  • Components: 3 major ICs
  • Total Cost: โ‚น1,060
  • Frame Rate: 30+ FPS
  • Latency: <50 ms

Built with โค๏ธ using STM32, CMSIS-DSP, and lots of โ˜•


๐Ÿ“Œ Quick Links


โญ Star this repo if you found it useful!

About

Real-time FFT-based Audio Spectrum Analyzer on STM32F411 | INMP441 mic + SSD1306 OLED | CMSIS-DSP 512-pt FFT | 8-band visualization

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

3 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages