get y values of z values in spectrogram

3 views (last 30 days)
Freeplot
Freeplot on 10 Apr 2020
Answered: Soumya on 24 Jun 2025
Hii there. I want to get the y-values for specific z-values from a spectrogram. I just want to get the y-values for (z > -10).
This is my code for plot the spectrogram:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% load a signal
[x, fs] = audioread('file.wav'); % load an audio file
x = x(:, 1); % get the first channel
% determine the signal parameters
xlen = length(x); % signal length
t = (0:xlen-1)/fs; % time vector
% analysis parameters
wlen = 1024; % window length (recomended to be power of 2)
nfft = 4*wlen; % number of fft points (recomended to be power of 2)
hop = wlen/4; % hop size
TimeRes = wlen/fs; % time resulution of the analysis (i.e., window duration), s
FreqRes = 2*fs/wlen; % frequency resolution of the analysis (using Hanning window), Hz
% time-frequency grid parameters
TimeResGrid = hop/fs; % time resolution of the grid, s
FreqResGrid = fs/nfft; % frequency resolution of the grid, Hz
% perform STFT
w1 = hanning(wlen, 'periodic');
[~, fS, tS, PSD] = spectrogram(x, w1, wlen-hop, nfft, fs);
Samp = 20*log10(sqrt(PSD.*enbw(w1, fs))*sqrt(2));
% plot the spectrogram
figure;
surf(tS, fS, Samp);
shading interp;
axis tight;
box on;
set(gca, 'FontName', 'Times New Roman', 'FontSize', 12);
ylim([0 1500]);
xlim([0 2]);
xlabel('Time, s');
ylabel('Frequency, Hz');
title('Unterarm Messung Ruhepuls');
view(0, 90);
colormap('jet');
hcol = colorbar;
set(hcol, 'FontName', 'Times New Roman', 'FontSize', 12)
ylabel(hcol, 'Magnitude, dB')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Answers (1)

Soumya
Soumya on 24 Jun 2025
Hi,
The ‘find’ function returns the indices of elements in an array that satisfy a specified condition. In the spectrogram matrix, where each element represents a magnitude value at a certain frequency and time, ‘find’ can be used to locate all points where the magnitude exceeds a threshold. This returns two sets of indices which include row indices corresponding to frequencies and column indices corresponding to times. You can then use these indices to extract the actual frequency and time values from the frequency vector and time vector, respectively.
The following steps will help you get the y-values for the specific z-values (here samp):
  • Use the ‘find’ function to find indices for the condition(samp>-10):
[row, col] = find(Samp > -10);
  • Extract corresponding y-values (frequencies) using the computed ‘row’ indexes from the ‘find’ function:
frequencies = fS(row);
Below is the output for a sample data:
Please refer to the following documentation links to know more about the ‘find’ function:
I hope this helps!

Categories

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

Community Treasure Hunt

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

Start Hunting!