Myspectrogram
该函数可以用于绘制语谱图,并通过实例演示如何调用,与matlab自带语谱图进行了对比。注意:文件名要和函数名保持一致!话不多说!有用记得点个赞。
函数:
function s = myspectrogram(audiodata, win, overlap, nfft, fs)
% audiodata 语音数据
% win 帧长或者窗
% overlap 帧与帧之间的重叠
% nfft DFT的点数
% fs 采样频率
%Short-Time Fourier Transform
%------------------dcj--Edit in 20200308-----------------------
dataL = length(audiodata(:)); % length of the signal
if(length(win)==1)
winL = win;
else
winL = length(win);
end
inc = winL - overlap;
fn = fix((dataL - overlap)/inc);
% form the stft matrix
rown = ceil((1+nfft)/2); % calculate the total number of rows
coln = 1+fix((dataL-winL)/inc); % calculate the total number of columns
stft = zeros(rown, coln); % form the stft matrix
% initialize the indexes
indx = 0;
col = 1;
% perform STFT
while indx + winL <= dataL
% windowing
dframe = audiodata(indx+1:indx+winL).*win;
% FFT
X = fft(dframe, nfft);
% update the stft matrix
stft(:, col) = X(1:rown);
% update the indexes
indx = indx + inc;
col = col + 1;
end
s =stft;
% calculate the time and frequency vectors
t = (winL/2:inc:winL/2+(coln-1)*inc)/fs;
f = (0:rown-1)*fs/nfft/1000;
%plot the spectrogram
%set(gcf,'Position',[20 100 600 500]);
%axes('Position',[0.1 0.1 0.85 0.5]);
% clims = [-140 -40];
imagesc(t,f,log(2*abs(stft/nfft))*10);%存在幅度修正
c = colorbar;
c.Label.String = 'power/frequency(dB/Hz)';
axis xy;
xlabel('Time/s');
ylabel('Frequency/KHz');
title('myspectrogram');
end
实例演示:
自己建一个新的文件,与其中调用的函数放在同一个路径,如test.m或test.mlx。
%using myspectrogram to present Spectrogram
[x,Fs] = audioread("clearspeech.wav");
y=resample(x,8000,Fs);
soundsc(y);
%subplot(312)
subplot(211)
spectrogram(y,hann(400),200,1400,8000,'yaixs');
title('spectrogram function of matlab');
subplot(212)
myspectrogram(y,hann(400),200,1400,8000);
title('Myspectrogram');
结果展示:
问题分析:
在这里,可以看到两张图在颜色深浅上有一些小区别,我通过stft的数据对比,结果是完全一样的。问题应该就出现在画图的那一段,如果你们解决了或者有什么问题,留言区见。