myspectrogram(自制语谱图函数)_matlab

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的数据对比,结果是完全一样的。问题应该就出现在画图的那一段,如果你们解决了或者有什么问题,留言区见。

  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值