This project provides a toolkit for analyzing audio signals in MATLAB. It loads an audio file, visualizes its waveform and spectrogram, and demonstrates the application of basic digital filters (low-pass and high-pass).
The project was written for a Mathematical Software course and practices MATLAB package development rules.
- Modular Package: All analysis logic (loading, plotting, filtering) is encapsulated in a
+analysisMATLAB package. - Separation of Concerns: Each function has a single purpose:
load_audio.m: Handles loading files (both built-in and user-specified) and ensures mono format.plot_waveform.m: Generates the time-domain plot.plot_spectrogram.m: Generates the time-frequency plot.apply_filter.m: Designs and applies digital filters.
- Data Handling: Demonstrates loading both built-in
.mataudio files (handel.mat) and standard.wavfiles usingaudioread. - Digital Signal Processing (DSP): Implements core DSP concepts, including the Short-Time Fourier Transform (via
spectrogram) and FIR filtering (viadesignfilt). - Publication-Ready Plots: All plotting functions return figure handles and save high-quality
.pngimages to anoutput/directory.
This project is built on a few fundamental concepts from Digital Signal Processing (DSP).
A digital audio signal is a time-domain representation. It's a long list of numbers (samples) that represent the amplitude (loudness) of the sound pressure wave at discrete, evenly-spaced moments in time.
-
Math: We plot Amplitude
$y$ as a function of time$t$ . -
Implementation: The file gives us the signal
$y$ (a vector of$N$ samples) and the sample rate$F_s$ (in samples per second, e.g., 44100 Hz). To get the time vector$t$ for our x-axis, we simply calculate:$$t = [0, 1/F_s, 2/F_s, \ldots, (N-1)/F_s]$$ This is done in
plot_waveform.mwith the vectorized code:t = (0:length(y)-1) / Fs;
The waveform tells us when the sound is loud or quiet, but not what the pitches (frequencies) are. A Fourier Transform (FFT) can convert the entire signal from the time-domain to the frequency-domain, telling us which frequencies were present in the whole file.
However, this is not very useful for music or speech, where the frequencies change over time. We need a way to see how frequency content changes over time. This is solved by the Short-Time Fourier Transform (STFT), which is the algorithm used to create a spectrogram.
The STFT breaks the "what, not when" problem of the FFT by following these steps:
- Windowing: Choose a small "window" size (e.g., 512 samples).
- Chunk & FFT: Take the first chunk of 512 samples from the signal. Apply an FFT to this chunk. This gives you the frequency content only for that first small slice of time. The result is one vertical line of the final spectrogram image.
- Slide the Window: Move the window forward by a "hop size" (e.g., 256 samples, which creates 50% overlap).
- Repeat: Take the new 512-sample chunk, apply an FFT, and store the result as the next vertical line in the spectrogram.
- Assemble: Continue this process until you've slid the window across the entire signal. The resulting 2D matrix (Time vs. Frequency vs. Intensity) is the spectrogram.
- Implementation: We don't have to code this manually. MATLAB's
spectrogramfunction does it all for us. We just need to give it the parameters:spectrogram(y, window, noverlap, nfft, Fs, 'yaxis');
window: The windowing function to use (we usehann(512)).noverlap: The "hop size" (we use 50% overlap).nfft: The number of points for the FFT.
Filtering is the process of attenuating (reducing the amplitude of) certain frequencies while letting others pass through.
-
Low-Pass Filter: Allows low frequencies to pass and blocks high frequencies. This makes the audio sound "muffled" or "duller," as it removes the bright, high-pitched "sizzle."
-
High-Pass Filter: Allows high frequencies to pass and blocks low frequencies. This makes the audio sound "tinny" or "thin," as it removes the bass and body.
-
Implementation: We use MATLAB's modern
designfiltfunction to create a filter "object." We specify the filter type (lowpassfir), aFilterOrder(which controls its "steepness"), theCutoffFrequency, and theSampleRate.lpFilt = designfilt('lowpassfir', ... 'FilterOrder', 70, ... 'CutoffFrequency', 4000, ... 'SampleRate', Fs);
Once the filter is designed, we apply it to our signal
$y$ using thefilterfunction:y_low = filter(lpFilt, y);
The spectrograms for
y_lowandy_highclearly show this effect: the low-pass version will have all the high-frequency content (the top half of the plot) blacked out, and the high-pass version will have the low-frequency content (the bottom half) blacked out.
matlab-audio-analysis-toolkit/
├── .gitignore # Ignores MATLAB temp files and 'output/' dir
├── LICENSE
├── README.md # This documentation
├── main.m # --- The main runnable script ---
└── +analysis/ # The MATLAB package folder
├── apply_filter.m # Function to design and apply a filter
├── load_audio.m # Helper function to load audio
├── plot_spectrogram.m # Function to plot the spectrogram
└── plot_waveform.m # Function to plot the time-domain waveform
-
Clone or Download: Get the project files onto your computer.
-
Open in MATLAB: Open the
matlab-audio-analysis-toolkitfolder in MATLAB. -
Run
main: Open themain.mscript and press the Run button, or type the following in the MATLAB Command Window:>> main Loading audio... Loaded built-in "handel.mat" audio file. Plotting waveform... Plotting spectrogram... Applying filters... Playing original audio... (audio plays) Playing low-pass filtered audio... (audio plays) Playing high-pass filtered audio... (audio plays) Plotting filtered spectrograms... Analysis complete. Check the /output folder for plots.
Feel free to connect or reach out if you have any questions!
- Maryam Rezaee
- GitHub: @msmrexe
- Email: ms.maryamrezaee@gmail.com
This project is licensed under the MIT License. See the LICENSE file for full details.