Skip to content

MATLAB-based project that applies FFT-based spectral subtraction to reduce background noise from audio recordings. Includes time-frequency visualizations and SNR evaluation.

Notifications You must be signed in to change notification settings

FechL/MATLAB-audio-denoising

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Audio Noise Reduction using FFT in MATLAB

This project demonstrates how to reduce background noise from an audio recording using frequency-domain filtering techniques based on Fast Fourier Transform (FFT) in MATLAB.

In real-world environments, audio recordings are often contaminated by background noise, such as wind, crowd chatter, or natural ambient sounds. This project implements a simple but effective method for denoising such audio using MATLAB and frequency analysis via FFT.

The goals of the project are:

  • Record a voice sample that contains background noise.
  • Apply FFT-based filtering to reduce noise and improve clarity.
  • Compare the signal before and after filtering both visually and analytically.

Usage

Requirements

  • MATLAB (R2020b or newer recommended)

How to Run

  1. Launch MATLAB and open the project script.

  2. Adjust the values of the alpha and beta parameters in the code

    alpha = 100; % Oversubtraction factor
    beta = 0.0000001; % Noise floor
    
  3. Run the main script:

    denoising.m
    

    make sure there is an audio input named record.wav placed in the working directory

  4. The script will generate:

    • Cleaned audio file: record_filtered.wav
    • Time-domain and frequency-domain plots of original vs filtered signals
    • SNR improvement log

Process Explanation

Workspace Initialization

The workspace and figures are cleared to avoid conflicts:

clear all;
close all;
clc;

Audio Reading and Preparation

The audio is read using audioread, and stereo audio is converted to mono if needed:

[audioIn, Fs] = audioread('record.wav');
if size(audioIn, 2) > 1
    audioIn = mean(audioIn, 2); % Convert stereo to mono
end

FFT Analysis Parameters

Several parameters are defined for efficient FFT processing:

  • Frame length: 2048 samples (a power of 2)
  • Hop size: frameLength / 4 → 75% overlap
  • Window function: Hanning window to reduce spectral leakage
frameLength = 2048;
hopSize = frameLength / 4;
window = hann(frameLength, 'periodic');

Noise Profile Estimation

Noise is estimated from the first 5 frames of audio under the assumption that they contain only noise:

Noise Profile Calculation:

noiseProfile = mean(abs(fft(noiseFrames .* window)));

Noise Reduction Parameters

The core parameters used for spectral subtraction:

  • Oversubtraction factor α = 100
  • Noise floor β = 1e-7
  • Minimum gain G_min = 0.05

Spectral Subtraction per Frame

Each frame undergoes the following steps:

(a) FFT Transformation

Convert frame to frequency domain:

frameFFT = fft(frame);
mag = abs(frameFFT);
phase = angle(frameFFT);

(b) Spectral Subtraction

Subtract estimated noise from magnitude spectrum:

magEnhanced = max(mag - α * noiseProfile, β * mag);

(c) Gain Mask Computation

$$G(f) = \frac{\max(mag - \alpha \cdot noiseProfile, \beta \cdot mag)}{mag}$$

With limiting:

gainMask = max(magEnhanced ./ max(mag, eps), G_min);

(d) Smoothing

Smooth the gain mask using moving average:

smoothFilter = ones(5,1)/5;
gainMask = filter(smoothFilter, 1, gainMask);

(e) Symmetry Enforcement

The second half of the spectrum mirrors the first (due to real-valued signals):

gainMask(half+1:end) = flipud(gainMask(2:end-1));

(f) Apply Gain and Reconstruct Signal

Apply gain to the original spectrum and perform inverse FFT:

enhancedFFT = gainMask .* frameFFT;
enhancedFrame = real(ifft(enhancedFFT));

(g) Overlap-Add Reconstruction

audioOut(startIdx:startIdx+frameLength-1) += enhancedFrame .* window;

Normalization

After all frames are processed, normalization compensates for overlapping windows:

audioOut(validIdx) = audioOut(validIdx) ./ normalizeCoeff(validIdx);

Final RMS normalization:

audioOut = audioOut * (rms(audioIn) / max(eps, rms(audioOut)));

Visualizations

Time-Domain Comparison

Compare raw and denoised signals to observe amplitude smoothing: Time Domain

Frequency-Domain Comparison

Observe reduction in high-frequency noise: Frequency Domain

Signal-to-Noise Ratio (SNR) Analysis

The improvement is evaluated using:

SNR formula:

$$SNR = 10 \cdot \log_{10} \left( \frac{P_{\text{signal}}}{P_{\text{noise}}} \right)$$

Where:

  • P_signal = mean(audioIn.^2)
  • P_noise = mean((audioIn - audioOut).^2)

Results:

  • Original SNR ≈ ~low
  • Enhanced SNR ≈ ~higher
  • Improvement logged in the console

Resources

About

MATLAB-based project that applies FFT-based spectral subtraction to reduce background noise from audio recordings. Includes time-frequency visualizations and SNR evaluation.

Topics

Resources

Stars

Watchers

Forks

Languages