Matlab音频MFCC计算:函数调用与绘图实现指导请求
How to Call Your MFCC Function, Process Audio, and Visualize Results in MATLAB
Got it, let's walk through building a complete main script to use your existing mfcc function. I'll cover loading audio, setting parameters, calling the function, and plotting the MFCC results clearly.
Step 1: Make Sure Dependencies Are Ready
Your mfcc function calls two helper functions: vec2frames and trifbank. Double-check these are in your MATLAB path (same folder as your mfcc.m and main script) — if you don't have them, you can find standard implementations online or write simple versions, but I'll assume they're available for this walkthrough.
Step 2: Full Main Script Code
Create a new .m file (e.g., run_mfcc.m) with this code:
% -------------------------- % Main Script to Compute and Plot MFCCs % -------------------------- % 1. Load your audio file % Replace 'your_audio.wav' with your actual audio file path [speech, fs] = audioread('your_audio.wav'); % If audio is stereo, convert to mono if size(speech, 2) > 1 speech = mean(speech, 2); end % 2. Set MFCC parameters (match the function's input requirements) Tw = 25; % Frame duration in milliseconds Ts = 10; % Frame shift in milliseconds alpha = 0.97; % Pre-emphasis coefficient window = @hamming; % Window function (hamming, hanning, etc.) R = 0; % Lowest frequency in Hz (usually 0) M = 26; % Number of mel filterbank channels N = 13; % Number of cepstral coefficients (including 0th) L = 22; % Cepstral lifter parameter % 3. Call your MFCC function [CC, FBE, frames] = mfcc(speech, fs, Tw, Ts, alpha, window, R, M, N, L); % 4. Visualize the MFCC results figure('Color','w'); % Plot MFCC spectrogram (transpose CC because columns are frames) subplot(2,1,1); imagesc(1:size(CC,2), 0:N-1, CC); axis xy; % Flip y-axis to show 0th cepstrum at the top colorbar; title('MFCC Coefficients'); xlabel('Frame Number'); ylabel('Cepstral Coefficient Index'); % Plot Filterbank Energies (FBE) if you want to see intermediate results subplot(2,1,2); imagesc(1:size(FBE,2), 1:M, FBE); axis xy; colorbar; title('Mel Filterbank Energies'); xlabel('Frame Number'); ylabel('Filterbank Channel Index'); % Optional: Plot a single frame's MFCC coefficients figure('Color','w'); plot(0:N-1, CC(:,100)); % Plot 100th frame's MFCCs title('MFCC Coefficients for Frame 100'); xlabel('Cepstral Coefficient Index'); ylabel('Amplitude'); grid on;
Step 3: What Each Part Does
Let's break down the key bits:
- Audio Loading:
audioreadhandles most common formats (WAV, MP3, etc.). We convert stereo to mono since MFCCs are typically computed on single-channel audio. - Parameter Setup: I used standard values for speech processing, but you can tweak these:
Tw=25andTs=10are industry standard for frame duration/shift.alpha=0.97is the default pre-emphasis coefficient to boost high frequencies.M=26filterbank channels andN=13cepstral coefficients are typical for speech recognition.
- Function Call: We pass all required parameters to your
mfccfunction, which returns three outputs:CC: The MFCC coefficients (sizeN x number_of_frames).FBE: The mel filterbank energies (intermediate step).frames: The windowed audio frames.
- Visualization:
imagescis perfect for plotting MFCCs as a heatmap — transposingCCmakes frames run along the x-axis, which is easier to interpret.axis xyflips the y-axis so the 0th cepstral coefficient (which captures overall energy) is at the top.- The optional single-frame plot helps you inspect individual MFCC values.
Step 4: Testing and Troubleshooting
- If you get an error about
vec2framesortrifbank, make sure those functions are in your MATLAB path. - If your audio is too quiet, you can normalize it before processing:
speech = speech / max(abs(speech)); - Adjust parameters like
MorNif you need more/less detail in your MFCCs.
内容的提问来源于stack exchange,提问作者yosra
相关产品推荐
相关产品推荐

