% Read the voice file. [x1,fs] = audioread('segment2.mp3');
%Convert the voice from double channels to single channel. iflength(size(x1))>1 x1 = x1(:,1); end;
%Plot the voice waveform in the time and frequency domains. figure(1); plot(x1); title('The voice in the time domain'); xlabel('Time(second)'); ylabel('Amplitude'); figure(2); y1 = fft(x1); y1 = fftshift(y1); derta_fs = fs/length(x1); plot([-fs/2:derta_fs:fs/2-derta_fs],abs(y1)/fs); title('The voice in the frequency domain'); xlabel('Frequency (Hz)'); ylabel('Amplitude'); grid on;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % The design of lowpass filter.
% Set the cutoff frequency of the lowpass filter to 4000Hz. fc1 = 4000;
%design of the lowpass filter. N1 = 2*pi*0.9/(0.1*pi); wc1 = 2*pi*fc1/fs; ifrem(N1,2) N1 = N1+1; end; Window = boxcar(N1+1); b1 = fir1(N1,wc1/pi,Window); figure(3); freqz(b1,1,512); title('The frequency response function of the lowpass filter');
%Process the voice using the lowpass filter. x1_low = filter(b1,1,x1); figure(4); plot(x1_low); title('The voice in the time domain after passing through the lowpass filter'); xlabel('Time(second)'); ylabel('Amplitude');
figure(5); plot([-fs/2:derta_fs:fs/2-derta_fs],abs(fftshift(fft(x1_low)))); title('The voice in the time domain after passing through the lowpass filter'); xlabel('Frequency(Hz)'); ylabel('Amplitude');
%Save the voice after passing through the lowpass filter. You can hear it. audiowrite('segment2AfterLowpassFilter.wav',x1_low, fs);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %Design of the highpass filter.
%set the cutoff frequency of the highpass filter. fc2 = 4000;
%Design the highpass filter. N2 = 2*pi*3.1/(0.1*pi); wc2 = 2*pi*fc2/fs; N2 = N2+mod(N2,2); Window = hanning(N2+1); b2 = fir1(N2,wc2/pi,'high',Window); figure(6); freqz(b2,1,512); title('The frequency response function of the highpass filter');
x1_high = filter(b2,1,x1); figure(7); plot(x1_high); title('The voice in the time domain after passing through the high pass filter'); xlabel('Time(second)'); ylabel('Amplitude');
figure(8); plot([-fs/2:derta_fs:fs/2-derta_fs],abs(fftshift(fft(x1_high)))); title('The voice in the frequency domain after passing through the high pass filter'); xlabel('Frequency(Hz)'); ylabel('Amplitude');
%save the voice after passing through the highpass filter. You can hear it. audiowrite('segment2AfterHighFilter.wav',x1_high, fs);
% Read the voice file. [x1,fs] = audioread('segment2.mp3');%x1和fs大概采集了声音信号和频率叭
%Convert the voice from double channels to single channel. iflength(size(x1))>1 x1 = x1(:,1); end; %这段函数的作用是将多声道的音频转换成单声道并对其做傅里叶变换。主要目的应该是减少计算量,因为一个声道就可以得到声音的特征曲线。
之后就是八个绘图函数,重复的部分不再作分析
图表一:原声音的时域
1 2 3 4 5 6
%Plot the voice waveform in the time and frequency domains. figure(1); %该函数的作用是建立第一张图表 plot(x1); %做原始语音信号的时域图形,默认横轴是时间 title('The voice in the time domain'); %输出图表的标题 xlabel('Time(second)'); %x代表的量及单位 ylabel('Amplitude'); %y代表的量
输出结果
图表二:原声音的频域
1 2 3 4 5 6 7 8 9
figure(2); y1 = fft(x1); % 通过快速傅里叶变换得到声音的振幅 y1 = fftshift(y1); %让正半轴部分和负半轴部分的图像分别关于各自的中心对称,因为直接用fft得出的数据与频率不是对应的 derta_fs = fs/length(x1); %设置频谱的间隔,分辨率 plot([-fs/2:derta_fs:fs/2-derta_fs],abs(y1)/fs); %画出原始语音信号的频谱图 title('The voice in the frequency domain'); xlabel('Frequency (Hz)'); ylabel('Amplitude'); grid on; %添加网格线
figure(3); freqz(b1,1,512); title('The frequency response function of the lowpass filter');
输出结果:
图表四:低通声音的时域
1 2 3 4 5 6
x1_low = filter(b1,1,x1); figure(4); plot(x1_low); title('The voice in the time domain after passing through the lowpass filter'); xlabel('Time(second)'); ylabel('Amplitude');
输出结果:
图表五:低通声音的频域
1 2 3 4 5
figure(5); plot([-fs/2:derta_fs:fs/2-derta_fs],abs(fftshift(fft(x1_low)))); title('The voice in the time domain after passing through the lowpass filter'); xlabel('Frequency(Hz)'); ylabel('Amplitude');