基于matlab实现对⼀段语⾳信号的处理,这些处理包括时域分析、频域分析、倒谱、能量谱和语谱图,并且可以将图形保存以便后续研究。 先⽤matlabGUI设计好界⾯
我的界⾯:
菜单界⾯
运⾏后于是就出现了这样的效果
下⼀步就是问题的核⼼为这些按钮添加回调函数callback
下⾯贴出我的程序和注释:
function varargout = sigany(varargin)
gui_Singleton = 1;山煤集团杜建华
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @sigany_OpeningFcn, ...
'gui_OutputFcn', @sigany_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
最后一分钟教学设计end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before sigany is made visible.
function sigany_OpeningFcn(hObject, ~, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);
function varargout = sigany_OutputFcn(~, ~, handles)
varargout{1} = handles.output;
% --- Executes on button press in start.
% --------------------------------------------------------------------
function init_Callback(hObject, eventdata, handles)%清理内存初始化
clear all;
clc;
% --------------------------------------------------------------------
function openexistdata_Callback(hObject, ~, handles)%打开⾳频,并显⽰时域波形[FileName,PathName]= uigetfile('*.wav'); %打开对话框windowsserver2003
[FileName,PathName]= uigetfile('*.wav'); %打开对话框
if ~isequal(FileName, 0)
% open(file);
[y, fs]=wavread([PathName FileName]);%x是⾳频的数据向量,fs是采样频率(单位Hz),bits是每⼀个采样点的数据深度(即⽐特数)end handles.data=y; %x是向量含有的所有数字的个数,与数据的⼤⼩有关
handles.sample=fs; %采样的频率⼀般为8k
t=length(handles.data)/fs; %采样的总时间
tt=0:t/length(handles.data):t; %中间计算出来的采样周期
handles.t=tt(1:length(tt)-1); %作为要显⽰图形的横坐标
guidata(hObject, handles); %保存更新
axes(handles.axes2); %打开的⽂件显⽰在第⼆个⾥
plot(handles.t,y); % ±ò¨
ylabel('signal Magnitude ');
xlabel('time(s)');
title('时域波形');
% --------------------------------------------------------------------
% --------------------------------------------------------------------
function wavsave_Callback(~, ~, handles) %时域保存
% hObject handle to wavsave (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[file,path] = uiputfile('wave.jpg','Save file name'); %打开保存窗⼝
%~isempty(handles.data);
h=figure; %画图
雷公藤多苷plot(handles.t,handles.data);
saveas(h,[path file]); %保存
close(h);
% --------------------------------------------------------------------
function savefftpic_Callback(~, ~, handles)%频谱保存
% hObject handle to savefftpic (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
Fs=handles.sample; % 采样率
data=handles.data; % 样本向量
blockSize=length(data); % 样本⼤⼩
xFFT = fft(data); % 快速傅⾥叶变换
xfft = abs(xFFT); % 离散傅⾥叶变换的幅值
index = xfft == 0;
xfft(index) = 1e-17;
mag = 20*log10(xfft); %20倍的log以10为底求对数
mag = mag(1:blockSize/2); %只取前⼀半
%原因:对实信号fft,产⽣了负频率,由于FFT变化是对称的,
%所以负频率对应到了Fs/2~Fs上,对实信号⽽⾔,该段频率没有任何意义,
%所以只显⽰到Fs/2。
%f = (0:length(mag)-1)*Fs/blockSize;
f = (0:length(mag)-1)*Fs/blockSize; %求横坐标频率序列
[file,path] = uiputfile('fft.jpg','Save file name'); %保存图⽚,同上
h=figure;
plot(f(:,1),mag(:,1));
ylabel('Magnitude (dB)');
xlabel('Frequency (Hz)');
xlabel('Frequency (Hz)');
title('频谱图');
saveas(h,[path file]);
close(h);
% --------------------------------------------------------------------
function picdaopusave_Callback(~, ~, handles)%倒谱保存
% hObject handle to picdaopusave (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
c=fft(log(abs(handles.data)+eps));% 取模取对数快速傅⾥叶求出倒谱 eps返回参数的精度ms1=handles.sample/1000;
ms20=handles.sample/50;
q=(ms1:ms20)/handles.sample; % 横坐标时间序列
[file,path] = uiputfile('daopu.jpg','Save file name');
h=figure; %作图保存
plot(q,abs(c(ms1:ms20)));
xlabel('倒⾓');
ylabel('倒谱幅度');
title('倒谱图');
saveas(h,[path file]);
close(h);
% --------------------------------------------------------------------
function picyupusave_Callback(~, ~, handles) %语谱图保存
% hObject handle to picyupusave (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
Winsiz=256;Shift=128;Base=0;%帧长帧移植基准电平值
signal=handles.data;
fs=handles.sample;
nseg=floor((length(signal)-Winsiz)/Shift)+1; %floor 朝负⽆穷⽅向舍⼊
A=zeros(Winsiz/2+1,nseg);
%下⾯循环是x信号的加窗处理并求出各点频谱能量
for i=1:nseg
n1=(i-1)*Shift+1;
n2=n1+(Winsiz-1);
xx=signal(n1:n2);
xx=xx.*hamming(Winsiz);
y=fft(xx);
y=y(1:Winsiz/2+1);
y=y.*conj(y);
y=10*log10(y);
A(:,i)=y;
end
L1=(A>Base);
L0=(A<Base);
B=A.*L1+Base*L0;
C=(B-Base)./(max(max(B))-Base);%灰度
y=[0:Winsiz/2]*fs/Winsiz; %纵坐标频率
x=[0:nseg-1]*Shift; %横坐标时间
%保存
[file,path] = uiputfile('yuputu.jpg','Save file name');
h=figure;
imagesc(x,y,C);axis xy;五洲国际码头
saveas(h,[path file]);
saveas(h,[path file]);
close(h);
function picsaveglp_Callback(~, ~, handles)%功率谱保存
% hObject handle to picsaveglp (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) Fs=handles.sample; %
xn=handles.data;
window=rectwin(length(xn)); %矩形加窗
nfft=1024;
[Pxx,f]=periodogram(xn,window,nfft,Fs); %求功率谱的
[file,path] = uiputfile('gonglvpu.jpg','Save file name');%保存⽂件
h=figure;
plot(f,10*log10(Pxx));
title('功率谱');
saveas(h,[path file]);
close(h);
% --- Executes on button press in wavepos.
function wavepos_Callback(~, ~, handles) %时域回调
% hObject handle to wavepos (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) y=handles.data; % ù
axes(handles.axes2); % èúò
plot(handles.t,y); % 时域直接表⽰即可
ylabel('signal Magnitude ');
xlabel('time(s)');
title('时域波形');
function fftplot_Callback(~, ~, handles)%频域回调
% hObject handle to fftplot (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) Fs=handles.sample; % ù
data=handles.data; % ù
blockSize=length(data);
华立通信
xFFT = fft(data); % 快速傅⾥叶变换
xfft = abs(xFFT); % 取模值
%index = xfft == 0;
%xfft(index) = 1e-17;
mag = 20*log10(xfft); % 转化成以dB为单位的数值
mag = mag(1:blockSize/2);
f = (0:length(mag)-1)*Fs/blockSize;
axes(handles.axes2);
plot(f(:,1),mag(:,1));
ylabel('Magnitude (dB)');
xlabel('Frequency (Hz)');
title('频谱图');