写一个原始信号是时域信号,纵轴是经过以10为底对数变换后的频率轴,横轴是时间的语谱图matlab代码
时间: 2024-03-04 18:48:52 浏览: 160
以下是在MATLAB中实现该问题的代码:
```matlab
% 读取音频文件
[x, Fs] = audioread('test.wav');
% 预加重
pre_emphasis = 0.97;
emphasized_signal = filter([1 -pre_emphasis], 1, x);
% 分帧
frame_size = 0.025; % 帧长25ms
frame_stride = 0.01; % 帧移10ms
frame_length = round(frame_size * Fs);
frame_step = round(frame_stride * Fs);
signal_length = length(emphasized_signal);
num_frames = ceil(abs(signal_length - frame_length) / frame_step);
pad_signal_length = num_frames * frame_step + frame_length;
z = zeros(1, pad_signal_length - signal_length);
pad_signal = [emphasized_signal, z];
indices = repmat(1:frame_length, num_frames, 1) + repmat((0:num_frames-1)' * frame_step, 1, frame_length);
frames = pad_signal(indices);
% 加窗处理
window = hamming(frame_length);
frames = frames .* repmat(window', size(frames, 1), 1);
% STFT
NFFT = 512; % FFT窗口大小
spectrogram = abs(fft(frames, NFFT)).^2;
freqs = (0:floor(NFFT/2))*(Fs/NFFT);
times = (0:size(frames, 1)-1)*(frame_step/Fs);
% 对数变换
spectrogram = 10*log10(spectrogram);
% 绘制语谱图
figure;
imagesc(times, freqs, spectrogram(:, 1:floor(NFFT/2)+1)');
axis xy;
xlabel('Time [sec]');
ylabel('Frequency [Hz]');
colormap(jet);
colorbar;
```
其中,test.wav是要处理的音频文件。绘制出来的图像的纵轴是以10为底数的对数变换后的频率轴,横轴是时间轴,即语谱图。
阅读全文