19.完善程序:(1)%加矩形窗时的倒谱和复倒谱clear;fid=fopen('sx86.txt','r');a=fscanf(fid,'%f')fclose(fid);N=300;h=linspace(1,1,N);for m=1:N
时间: 2023-06-14 07:06:47 浏览: 145
%加矩形窗时的倒谱和复倒谱
clear;
fid=fopen('sx86.txt','r');
a=fscanf(fid,'%f');
fclose(fid);
N=300;
h=ones(1,N); %使用矩形窗
%计算倒谱
A = log(abs(fft(h.*a)));
cepstrum = ifft(A);
%计算复倒谱
B = abs(fft(h.*a));
complex_cepstrum = ifft(log(B));
%绘图
subplot(2,1,1)
plot(cepstrum)
title('倒谱')
xlabel('样本数')
ylabel('幅值')
subplot(2,1,2)
plot(complex_cepstrum)
title('复倒谱')
xlabel('样本数')
ylabel('幅值')
相关问题
%对语音信号进行截短的代码 %加矩形窗时的倒谱的代码 %加矩形窗时的复倒谱的代码
对语音信号进行截短的代码:
```python
import librosa
# load audio signal
signal, sr = librosa.load('audio.wav')
# set start and end time in seconds
start_time = 1.0
end_time = 3.0
# convert to sample indices
start_index = int(start_time * sr)
end_index = int(end_time * sr)
# extract the truncated signal
truncated_signal = signal[start_index:end_index]
```
加矩形窗时的倒谱的代码:
```python
import numpy as np
import librosa
# load audio signal
signal, sr = librosa.load('audio.wav')
# set window size and hop length
window_size = 1024
hop_length = 512
# apply rectangular window
signal_windowed = signal * np.hamming(window_size)
# calculate magnitude spectrum
magnitude_spectrum = np.abs(librosa.stft(signal_windowed, n_fft=window_size, hop_length=hop_length))
# calculate power spectrum
power_spectrum = magnitude_spectrum ** 2
# apply mel filterbank
mel_basis = librosa.filters.mel(sr=sr, n_fft=window_size, n_mels=40)
mel_spectrum = np.dot(mel_basis, power_spectrum)
# calculate log mel spectrum
log_mel_spectrum = librosa.power_to_db(mel_spectrum)
# calculate cepstral coefficients
cepstral_coefficients = librosa.feature.mfcc(S=log_mel_spectrum, n_mfcc=13)
# apply lifter to cepstral coefficients
lifter = 22
lifter_vector = 1 + (lifter / 2) * np.sin(np.pi * np.arange(cepstral_coefficients.shape[0]) / lifter)
lifter_matrix = np.tile(lifter_vector[:, np.newaxis], (1, cepstral_coefficients.shape[1]))
cepstral_coefficients_lifted = cepstral_coefficients * lifter_matrix
```
加矩形窗时的复倒谱的代码:
```python
import numpy as np
import librosa
# load audio signal
signal, sr = librosa.load('audio.wav')
# set window size and hop length
window_size = 1024
hop_length = 512
# apply rectangular window
signal_windowed = signal * np.hamming(window_size)
# calculate magnitude spectrum
magnitude_spectrum = np.abs(librosa.stft(signal_windowed, n_fft=window_size, hop_length=hop_length))
# calculate phase spectrum
phase_spectrum = np.angle(librosa.stft(signal_windowed, n_fft=window_size, hop_length=hop_length))
# calculate power spectrum
power_spectrum = magnitude_spectrum ** 2
# apply mel filterbank
mel_basis = librosa.filters.mel(sr=sr, n_fft=window_size, n_mels=40)
mel_spectrum = np.dot(mel_basis, power_spectrum)
# calculate log mel spectrum
log_mel_spectrum = librosa.power_to_db(mel_spectrum)
# calculate cepstral coefficients
cepstral_coefficients = librosa.feature.mfcc(S=log_mel_spectrum, n_mfcc=13)
# calculate lifter
lifter = 22
lifter_vector = 1 + (lifter / 2) * np.sin(np.pi * np.arange(cepstral_coefficients.shape[0]) / lifter)
# apply lifter to cepstral coefficients
lifter_matrix = np.tile(lifter_vector[:, np.newaxis], (1, cepstral_coefficients.shape[1]))
cepstral_coefficients_lifted = cepstral_coefficients * lifter_matrix
# apply inverse DCT to liftered cepstral coefficients
cepstral_coefficients_lifted_idct = np.real(np.fft.ifft(cepstral_coefficients_lifted, axis=0))
# apply exponential to liftered cepstral coefficients
cepstral_coefficients_lifted_exponential = np.exp(cepstral_coefficients_lifted_idct)
# apply inverse Fourier transform to exponential of liftered cepstral coefficients
cepstral_coefficients_lifted_exponential_ifft = np.fft.ifft(cepstral_coefficients_lifted_exponential, axis=0)
# calculate real part of complex cepstral coefficients
real_part = np.real(cepstral_coefficients_lifted_exponential_ifft)
# calculate imaginary part of complex cepstral coefficients
imaginary_part = np.imag(cepstral_coefficients_lifted_exponential_ifft)
# combine real and imaginary parts to form complex cepstral coefficients
complex_cepstral_coefficients = real_part + 1j * imaginary_part
# apply Fourier transform to complex cepstral coefficients
cepstral_coefficients_complex_fft = np.fft.fft(complex_cepstral_coefficients, axis=0)
# calculate phase spectrum of complex cepstral coefficients
phase_spectrum_complex_cepstral = np.angle(cepstral_coefficients_complex_fft)
# calculate magnitude spectrum of phase spectrum of complex cepstral coefficients
magnitude_spectrum_phase_spectrum_complex_cepstral = np.abs(librosa.stft(phase_spectrum_complex_cepstral, n_fft=window_size, hop_length=hop_length))
# calculate complex cepstral coefficients of phase spectrum of complex cepstral coefficients
cepstral_coefficients_phase_spectrum_complex_cepstral = np.fft.ifft(np.log(magnitude_spectrum_phase_spectrum_complex_cepstral), axis=0)
# apply exponential to complex cepstral coefficients of phase spectrum of complex cepstral coefficients
cepstral_coefficients_phase_spectrum_complex_cepstral_exponential = np.exp(cepstral_coefficients_phase_spectrum_complex_cepstral)
# apply Fourier transform to exponential of complex cepstral coefficients of phase spectrum of complex cepstral coefficients
cepstral_coefficients_phase_spectrum_complex_cepstral_exponential_fft = np.fft.fft(cepstral_coefficients_phase_spectrum_complex_cepstral_exponential, axis=0)
# calculate complex cepstral coefficients of phase spectrum of complex cepstral coefficients
complex_cepstral_coefficients_phase_spectrum_complex_cepstral = np.real(cepstral_coefficients_phase_spectrum_complex_cepstral_exponential_fft)
# calculate real cepstral coefficients of phase spectrum of complex cepstral coefficients
real_cepstral_coefficients_phase_spectrum_complex_cepstral = np.real(np.fft.ifft(complex_cepstral_coefficients_phase_spectrum_complex_cepstral, axis=0))
# apply lifter to real cepstral coefficients of phase spectrum of complex cepstral coefficients
lifter_phase_spectrum_complex_cepstral = 22
lifter_vector_phase_spectrum_complex_cepstral = 1 + (lifter_phase_spectrum_complex_cepstral / 2) * np.sin(np.pi * np.arange(real_cepstral_coefficients_phase_spectrum_complex_cepstral.shape[0]) / lifter_phase_spectrum_complex_cepstral)
lifter_matrix_phase_spectrum_complex_cepstral = np.tile(lifter_vector_phase_spectrum_complex_cepstral[:, np.newaxis], (1, real_cepstral_coefficients_phase_spectrum_complex_cepstral.shape[1]))
real_cepstral_coefficients_phase_spectrum_complex_cepstral_lifted = real_cepstral_coefficients_phase_spectrum_complex_cepstral * lifter_matrix_phase_spectrum_complex_cepstral
# apply lifter to real cepstral coefficients
lifter = 22
lifter_vector = 1 + (lifter / 2) * np.sin(np.pi * np.arange(cepstral_coefficients.shape[0]) / lifter)
lifter_matrix = np.tile(lifter_vector[:, np.newaxis], (1, cepstral_coefficients.shape[1]))
cepstral_coefficients_lifted = cepstral_coefficients * lifter_matrix
# combine real cepstral coefficients and real cepstral coefficients of phase spectrum of complex cepstral coefficients
combined_cepstral_coefficients = np.concatenate((cepstral_coefficients_lifted, real_cepstral_coefficients_phase_spectrum_complex_cepstral_lifted), axis=0)
```
编写完整程序:matlab提取声道倒谱参数 1.用计算机采集一段语音 2.提取声道倒谱参数
在MATLAB中,提取声道倒谱参数通常涉及到音频信号处理步骤。以下是编写一个简化的示例程序,它包括采集语音数据和提取梅尔频率倒谱系数(Mel-frequency cepstral coefficients, MFCCs)作为声道倒谱参数:
```Matlab
% 1. 用计算机采集一段语音
% 这里假设你已经有了音频文件,如果需要实时采集,可以使用audiorec命令
[audioFile, Fs] = audioread('your_audio_file.wav'); % 替换 'your_audio_file.wav' 为实际文件名
audioData = audioFile; % 如果音频文件只包含单声道
% 2. 提取声道倒谱参数(MFCC)
% 首先对信号做预处理(归一化、窗口滑动等)
winSize = 0.04; % 窗口大小(秒)
hopSize = 0.01; % 滑动步长(秒)
window = hamming(winSize*Fs); % 使用汉明窗
nFFT = round(Fs * winSize);
% 分帧并计算STFT(快速傅立叶变换)
frameSize = nFFT;
frames = buffer(audioData, frameSize, hopSize);
numFrames = height(frames);
% 计算梅尔滤波器组
melBanks = melBank(numFrames, nFFT, Fs); % 根据需要调整mel滤波器的数量
% 提取MFCC
mfccs = zeros(1, melBanks(end, 2)); % 初始化结果向量
for i = 1:numFrames
stftFrame = frames{i};
magSpectrum = abs(stftFrame).^2; % 取绝对值后平方
logMagSpectrum = log(magSpectrum + eps); % 对数压缩
mfccs = [mfccs; dct(logMagSpectrum * melBanks(i, :))]; % DCT表示梅尔倒谱系数
end
mfccs = mfccs(2:end); % 删除直流分量
% 结果保存或后续分析
mfccVector = mfccs';
%
阅读全文