Frame by frame freqeuncy analysis of voice.

5 views (last 30 days)
below code for frame by frame analysis of wav file , Seniors Go through it and answer my question please , question is written in the last.
[data,fs]= audioread('filename.wav);
fd=0.025 % duration selected from 5 sec
f_size= round(fd*fs);% how many samples in a frame
n_f=floor(length(data)/f_size) %how many frames are there
temp=0; %temporary value
for i= 1: n_f
frames(i,:) =data (temp+1: temp+f_size);
temp=temp+f_size;
end
plot (frame(1,:))%plot first frame or any you want give there numbe .
now.my question is that how can I read frequency of that particular frame or that duration of 0.025 ms? please help me for it .

Accepted Answer

Bjorn Gustavsson
Bjorn Gustavsson on 15 Feb 2019
Have a look at the documentation for the spectrogram function, that should do what you want. 0.025 is a rather short time so perhaps you can get something from calculating the instantaneous frequency - so have a look at that (and first the Hilbert transform too), this is likely a rough suggestion since we expect frequencies way higher than 20 Hz.
HTH
  6 Comments
Bjorn Gustavsson
Bjorn Gustavsson on 15 Feb 2019
Yes. The SPECTROGRAM function does what you need - perhaps not what you think you want, but what you need. Let us look at the help text for spectrogram:
SPECTROGRAM Spectrogram using a Short-Time Fourier Transform (STFT).
S = SPECTROGRAM(X) returns the spectrogram of the signal specified by
vector X in the matrix S. By default, X is divided into eight segments
with 50% overlap, each segment is windowed with a Hamming window. The
number of frequency points used to calculate the discrete Fourier
transforms is equal to the maximum of 256 or the next power of two
greater than the length of each segment of X.
If X cannot be divided exactly into eight segments, X will be truncated
accordingly.
S = SPECTROGRAM(X,WINDOW) when WINDOW is a vector, divides X into
segments of length equal to the length of WINDOW, and then windows each
segment with the vector specified in WINDOW. If WINDOW is an integer,
X is divided into segments of length equal to that integer value, and a
Hamming window of equal length is used. If WINDOW is not specified, the
default is used.
S = SPECTROGRAM(X,WINDOW,NOVERLAP) NOVERLAP is the number of samples
each segment of X overlaps. NOVERLAP must be an integer smaller than
WINDOW if WINDOW is an integer. NOVERLAP must be an integer smaller
than the length of WINDOW if WINDOW is a vector. If NOVERLAP is not
specified, the default value is used to obtain a 50% overlap.
S = SPECTROGRAM(X,WINDOW,NOVERLAP,NFFT) specifies the number of
frequency points used to calculate the discrete Fourier transforms.
If NFFT is not specified, the default NFFT is used.
S = SPECTROGRAM(X,WINDOW,NOVERLAP,NFFT,Fs) Fs is the sampling frequency
specified in Hz. If Fs is specified as empty, it defaults to 1 Hz. If
it is not specified, normalized frequency is used.
Each column of S contains an estimate of the short-term, time-localized
frequency content of the signal X. Time increases across the columns
of S, from left to right. Frequency increases down the rows, starting
at 0. If X is a length NX complex signal, S is a complex matrix with
NFFT rows and k = fix((NX-NOVERLAP)/(length(WINDOW)-NOVERLAP)) columns.
For real X, S has (NFFT/2+1) rows if NFFT is even, and (NFFT+1)/2 rows
if NFFT is odd.
[S,F,T] = SPECTROGRAM(...) returns a vector of frequencies F and a
vector of times T at which the spectrogram is computed. F has length
equal to the number of rows of S. T has length k (defined above) and
its value corresponds to the center of each segment.
[S,F,T] = SPECTROGRAM(X,WINDOW,NOVERLAP,F,Fs) where F is a vector of
frequencies in Hz (with 2 or more elements) computes the spectrogram at
those frequencies using the Goertzel algorithm. The specified
frequencies in F are rounded to the nearest DFT bin commensurate with
the signal's resolution.
[S,F,T,P] = SPECTROGRAM(...) P is a matrix representing the Power
Spectral Density (PSD) of each segment. For real signals, SPECTROGRAM
returns the one-sided modified periodogram estimate of the PSD of each
segment; for complex signals and in the case when a vector of
frequencies is specified, it returns the two-sided PSD.
SPECTROGRAM(...) with no output arguments plots the PSD estimate for
each segment on a surface in the current figure. It uses
SURF(f,t,10*log10(abs(P)) where P is the fourth output argument. A
trailing input string, FREQLOCATION, controls where MATLAB displays the
frequency axis. This string can be either 'xaxis' or 'yaxis'. Setting
this FREQLOCATION to 'yaxis' displays frequency on the y-axis and time
on the x-axis. The default is 'xaxis' which displays the frequency on
the x-axis. If FREQLOCATION is specified when output arguments are
requested, it is ignored.
EXAMPLE 1: Display the PSD of each segment of a quadratic chirp.
t=0:0.001:2; % 2 secs @ 1kHz sample rate
y=chirp(t,100,1,200,'q'); % Start @ 100Hz, cross 200Hz at t=1sec
spectrogram(y,128,120,128,1E3); % Display the spectrogram
title('Quadratic Chirp: start at 100Hz and cross 200Hz at t=1sec');
EXAMPLE 2: Display the PSD of each segment of a linear chirp.
t=0:0.001:2; % 2 secs @ 1kHz sample rate
x=chirp(t,0,1,150); % Start @ DC, cross 150Hz at t=1sec
F = 0:.1:100;
[y,f,t,p] = spectrogram(x,256,250,F,1E3,'yaxis');
% NOTE: This is the same as calling SPECTROGRAM with no outputs.
surf(t,f,10*log10(abs(p)),'EdgeColor','none');
axis xy; axis tight; colormap(jet); view(0,90);
xlabel('Time');
ylabel('Frequency (Hz)');
See also PERIODOGRAM, PWELCH, SPECTRUM, GOERTZEL.
Reference page in Help browser
doc spectrogram
Now from the audio-read function you get the samples Y, and the sampling ferquency fs. Further, you want the temporal resolution of 0.025 s for your spectrum, so that would correpond to something like dt*fs number of samples. Lets just skip the NOVERLAP argument and the NFFT argument aso they will be defaulted. Then you calculate the spectrogram as
NW = round(fs*0.025);
[S,F,T,P] = spectrogram(Y,NW,[],[],fs);
% Then to plot:
subplot(2,1,1),pcolor(T,F,log10(P)),
shading flat,
caxis([-6 0]+max(caxis))
colorbar
subplot(2,1,2)
semilogy(F,P(:,23))
title(sprintf('PSD at time: %f',T(23)))
xlabel('Frequency (Hz)')
HTH
Yasir Ali
Yasir Ali on 16 Feb 2019
dear it shows error while I run this line of code
the error is "" Erroe using spectrogram>chkinput X must be a vectoe (either row or column ).
Error in Spectrogram Chkinput (x); ""
what to do to solve that error , may I have your email Id as I can get in touch with you through email please ?

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!