Clear Filters
Clear Filters

Ploting a spectrogrma view from sample points in an audio signal

4 views (last 30 days)
I extracted some samples from an audio signal. I am to plot and have the output in a spectrogram format without using the spectrogram function in Matlab
I arranged the signal in a matrix with eaxh column representing a frame of sample size 8192, I have 20 frames extracted 20 frames as shown below.
How do I go further from here.
[signal, Fs] = audioread ("data.wav");
fl = 8192; % frame length
% frame = signal (1:8192); % the frame is the window size.
Overlap = 0.5 % window overlap
for i = 1:20
frame = signal(((i-1)*fl + 1):i*fl)
frame_signal (:,i) = (frame);
end

Answers (1)

Sai Pavan
Sai Pavan on 12 Apr 2024
Hello,
I understand that you want to plot the spectogram of an audio signal without using the inbuilt MATLAB function.
We can utilize the Fast Fourier Transform (FFT) to compute the frequency spectrum for each frame and then plot these spectra to visualize the spectrogram. Please refer to the below workflow to perform this task:
  1. For each column in the "frame_signal" matrix, compute the FFT. This gives us the frequency content of each frame.
  2. Since the FFT results are complex, calculate the magnitude to get the amplitude spectrum.
  3. Plot the magnitude of the FFT for each frame as a function of frequency.
Please refer to the code snippet attached below that illustrates the workflow mentioned above:
[signal, Fs] = audioread("data.wav");
fl = 8192; % frame length
Overlap = 0.5; % window overlap
nFrames = 20; % Number of frames
frame_signal = zeros(fl, nFrames); % Preallocate matrix for speed
for i = 1:nFrames % Extract frames
frame_start = round((i-1)*fl*Overlap) + 1;
frame_end = frame_start + fl - 1;
if frame_end > length(signal)
break; % Avoid exceeding the signal length
end
frame_signal(:,i) = signal(frame_start:frame_end);
end
nFFT = fl; % Number of FFT points
freqResolution = Fs/nFFT;
freqAxis = (0:nFFT-1)*freqResolution;
fftMatrix = zeros(nFFT/2+1, nFrames); % Only half+1 due to symmetry of FFT
% Compute FFT for each frame and store the magnitude
for i = 1:nFrames
frameFFT = fft(frame_signal(:,i), nFFT);
fftMatrix(:,i) = abs(frameFFT(1:nFFT/2+1)); % Take half due to symmetry
end
figure;
imagesc([1 nFrames], freqAxis(1:nFFT/2+1), 20*log10(fftMatrix+1)); % Using log scale for magnitude
axis xy; % Flip the axis so lower frequencies are at the bottom
xlabel('Frame Number');
ylabel('Frequency (Hz)');
title('Spectrogram');
colorbar;
Hope it helps!

Categories

Find more on Time-Frequency Analysis in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!