⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 no4.m

📁 this program deals with homomorphic analysis of speech. it was homework assignment in digital speech
💻 M
字号:
function varargout = no4(varargin)% NO4 M-file for no4.fig%      NO4, by itself, creates a new NO4 or raises the existing%      singleton*.%%      H = NO4 returns the handle to a new NO4 or the handle to%      the existing singleton*.%%      NO4('CALLBACK',hObject,eventData,handles,...) calls the local%      function named CALLBACK in NO4.M with the given input arguments.%%      NO4('Property','Value',...) creates a new NO4 or raises the%      existing singleton*.  Starting from the left, property value pairs are%      applied to the GUI before no4_OpeningFcn gets called.  An%      unrecognized property name or invalid value makes property application%      stop.  All inputs are passed to no4_OpeningFcn via varargin.%%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one%      instance to run (singleton)".%% See also: GUIDE, GUIDATA, GUIHANDLES% Edit the above text to modify the response to help no4% Last Modified by GUIDE v2.5 07-Oct-2008 17:42:56% Begin initialization code - DO NOT EDITgui_Singleton = 1;gui_State = struct('gui_Name',       mfilename, ...    'gui_Singleton',  gui_Singleton, ...    'gui_OpeningFcn', @no4_OpeningFcn, ...    'gui_OutputFcn',  @no4_OutputFcn, ...    'gui_LayoutFcn',  [] , ...    'gui_Callback',   []);if nargin && ischar(varargin{1})    gui_State.gui_Callback = str2func(varargin{1});endif 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 no4 is made visible.function no4_OpeningFcn(hObject, eventdata, handles, varargin)% This function has no output args, see OutputFcn.% hObject    handle to figure% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)% varargin   command line arguments to no4 (see VARARGIN)% Choose default command line output for no4handles.output = hObject;handles.RadioButtonNumber = 2;handles.LPInput = 0;handles.LifterInput = 0;load hw2;handles.speech1 = speech1_10k';handles.speech2 = speech2_10k;handles.speech3 = speech3_10k;handles.f0_speech2 = f0_speech2;handles.f0_speech3 = f0_speech3;handles.Fs = 10000;handles.NFFT = 1024;% Update handles structureguidata(hObject, handles);set(handles.uipanel2,'SelectionChangeFcn',@uipanel2_SelectionChangeFcn);% --- Outputs from this function are returned to the command line.function varargout = no4_OutputFcn(hObject, eventdata, handles)% varargout  cell array for returning output args (see VARARGOUT);% hObject    handle to figure% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)% Get default command line output from handles structurevarargout{1} = handles.output;% --- Executes on button press in radiobutton1.function radiobutton1_Callback(hObject, eventdata, handles)% hObject    handle to radiobutton1 (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)% Hint: get(hObject,'Value') returns toggle state of radiobutton1% --- Executes on button press in radiobutton2.function radiobutton2_Callback(hObject, eventdata, handles)% hObject    handle to radiobutton2 (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)% Hint: get(hObject,'Value') returns toggle state of radiobutton2% --- Executes on button press in pushbutton1.function pushbutton1_Callback(hObject, eventdata, handles)% hObject    handle to pushbutton1 (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)handles.LPInput = str2double(get(handles.edit1,'String'));handles.LifterInput = str2double(get(handles.edit2,'String'));if handles.LifterInput > 12.5,    error('myApp:argChk', 'Liftering Window Length can''t be greater than length of signal');endif handles.RadioButtonNumber == 1,    speech = handles.speech2;    f0_speech = handles.f0_speech2;else speech = handles.speech3;    f0_speech = handles.f0_speech3;endaxes(handles.axes1)plot(linspace(1,length(speech)/handles.Fs, length(speech)),speech);title('Speech');xlabel('time(s)'); ylabel('Amplitude');axes(handles.axes2)index1 = handles.Fs/300;  %max freq = 500Hzindex2 = handles.Fs/50;   %max freq = 50Hz% range for searching maximawindowLength = 25e-3 * handles.Fs;frameLength = 25e-3*handles.Fs;numberFrame = length(speech) / frameLength;for Tau = 1:numberFrame;%     wind = rectwin(windowLength);    initialIndex = 1 + (Tau -1)* frameLength;    if (initialIndex+windowLength-1) > length(speech),        break;    end    Y = fft(speech(initialIndex:initialIndex+windowLength-1), handles.NFFT);    C=abs(ifft(log(abs(Y(:,:))),handles.NFFT));        [junk,locs]=max(C(index1:index2,1));    pitch(Tau,1) = handles.Fs/(index1+locs-1);    if junk < 0.09, pitch(Tau,1) = 0;endendplot(1:length(pitch),pitch,'b.', 1:length(pitch), f0_speech+30,'r.'); title('Pitch');xlabel('Frames');ylabel('Frequency');axes(handles.axes3)lengthPitch = length(pitch);% sourceExcitation = zeros(250*lengthPitch,1);sourceExcitation =[];for m = 1:lengthPitch,    %     q = 1 + (m-1)*250;    if pitch(m) == 0,        sourceExcitation =[sourceExcitation 0.1*randn(1,250)];    else        junk = zeros(1,250);        gap = floor(frameLength/(25e-3*pitch(m)));        for n = 1:gap:250,            junk(1,n) = 1;        end        sourceExcitation  = [sourceExcitation junk];    endendsourceExcitation = sourceExcitation';stem(sourceExcitation);title('Source Excitation');xlabel('Samples');ylabel('Amplitude');% axis([1 2000 -.25 1.25])axes(handles.axes4)for m = 1:numberFrame,    q = 1 + (m-1)*frameLength;    [a,E] = my_durbin(speech(q:q+frameLength-1),handles.LPInput);    b = 1;    reconLPC(q:q+frameLength-1) = filter(b,a, E*sourceExcitation(q:q+frameLength-1));endhandles.reconLPC =reconLPC';plot(linspace(1,length(handles.reconLPC)/handles.Fs, length(handles.reconLPC)),handles.reconLPC);title('Reconstructed Speech using LPC');xlabel('Time(s)');ylabel('Amplitude');axes(handles.axes5)reconCepstrum = zeros(1,frameLength*numberFrame+handles.NFFT-1);for m = 1:numberFrame,    q = 1+(m-1)*frameLength;    Y0=fft(speech(q:q+frameLength-1), handles.NFFT);    C=(ifft(log((Y0)),handles.NFFT));    d = [ones(1,ceil(handles.NFFT/25*handles.LifterInput))...    zeros(1,handles.NFFT - 2*ceil(handles.NFFT/25*handles.LifterInput))...    ones(1,ceil(handles.NFFT/25*handles.LifterInput))]';    windowedC = C.*d;    Y1 = fft(windowedC,handles.NFFT);    y = ifft(exp(Y1),handles.NFFT);    convolution = conv(sourceExcitation(q:q+frameLength-1,1),y);    convolution = convolution';    initialzeros = zeros(1, (m-1)*frameLength);    finalzeros = zeros(1,length(reconCepstrum)-length(convolution)- length(initialzeros));    reconCepstrum = reconCepstrum + ...        [initialzeros convolution finalzeros];   endhandles.reconCepstrum = reconCepstrum(1, 1:frameLength*numberFrame)';% reconCepstrum = reconCepstrum(1,1:frameLength*numberFrame)';plot(linspace(1,length(handles.reconCepstrum)/handles.Fs,...    length(handles.reconCepstrum)), handles.reconCepstrum); title('Reconstructed Speech using Cepstrum');ylabel('Amplitude'); xlabel('Time(s)'); xlim([1 length(handles.reconCepstrum)/handles.Fs]);guidata(hObject, handles);% --- Executes on button press in pushbutton2.function pushbutton2_Callback(hObject, eventdata, handles)% hObject    handle to pushbutton2 (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)if handles.RadioButtonNumber == 1,    sound(handles.speech2, handles.Fs);else sound(handles.speech3,handles.Fs);endsound(real(handles.reconLPC),handles.Fs);sound(real(handles.reconCepstrum),10000)% --- Executes on button press in pushbutton3.function pushbutton3_Callback(hObject, eventdata, handles)% hObject    handle to pushbutton3 (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)handles.LPInput = str2double(get(handles.edit1,'String'));handles.LifterInput = str2double(get(handles.edit2,'String'));if handles.LifterInput > 12.5,    error('myApp:argChk', 'Liftering Window Length can''t be greater than length of signal');endaxes(handles.axes1);plot((1:length(handles.speech1))/handles.Fs*1e3,handles.speech1);title('speech1');xlim([0 25]);xlabel('time(ms)'); ylabel('Amplitude');Y = log10(abs(fft(handles.speech1, handles.NFFT)));[a,E] = my_durbin(handles.speech1,handles.LPInput);b = 1;c = log10(abs(freqz(b,a)));axes(handles.axes3);Y0=fft(handles.speech1, handles.NFFT);C=(ifft(log(abs(Y0)),handles.NFFT));plot(linspace(1,25,handles.NFFT),C);hold on;title('speech1 cepstrum');xlabel('Quefrency(ms)');ylabel('Amplitude');d = [ones(1,ceil(handles.NFFT/25*handles.LifterInput))...    zeros(1,handles.NFFT - 2*ceil(handles.NFFT/25*handles.LifterInput))...    ones(1,ceil(handles.NFFT/25*handles.LifterInput))]';plot(linspace(1,25,handles.NFFT),d,'r');% axis([1 12.5 -0.5 0.5])hold off;d = zeros(1,handles.NFFT);% d = [ones(1,ceil(handles.NFFT/25*handles.LifterInput))...%     zeros(1,handles.NFFT - ceil(handles.NFFT/25*handles.LifterInput))]';d = [ones(1,ceil(handles.NFFT/25*handles.LifterInput))...    zeros(1,handles.NFFT - 2*ceil(handles.NFFT/25*handles.LifterInput))...    ones(1,ceil(handles.NFFT/25*handles.LifterInput))]';windowedC = C.*d;Y1 = fft(windowedC,handles.NFFT);Y2 = ifft(exp(Y1),handles.NFFT);Y3= log10(abs(fft(Y2,handles.NFFT)));axes(handles.axes2);plot(linspace(1,5,512),(Y(1:end/2)),'y',linspace(1,5,512),c,'r',...    linspace(1,5,512),Y3(1:end/2),'g');legend('Spec','LPC','Cep');xlabel('freq(kHz)'); ylabel('Magnitude');title('speech1');axis([1 5 -2 2]);guidata(hObject, handles);function edit1_Callback(hObject, eventdata, handles)% hObject    handle to edit1 (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)% Hints: get(hObject,'String') returns contents of edit1 as text%        str2double(get(hObject,'String')) returns contents of edit1 as a double% --- Executes during object creation, after setting all properties.function edit1_CreateFcn(hObject, eventdata, handles)% hObject    handle to edit1 (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    empty - handles not created until after all CreateFcns called% Hint: edit controls usually have a white background on Windows.%       See ISPC and COMPUTER.if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))    set(hObject,'BackgroundColor','white');endfunction edit2_Callback(hObject, eventdata, handles)% hObject    handle to edit2 (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)% Hints: get(hObject,'String') returns contents of edit2 as text%        str2double(get(hObject,'String')) returns contents of edit2 as a double% --- Executes during object creation, after setting all properties.function edit2_CreateFcn(hObject, eventdata, handles)% hObject    handle to edit2 (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    empty - handles not created until after all CreateFcns called% Hint: edit controls usually have a white background on Windows.%       See ISPC and COMPUTER.if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))    set(hObject,'BackgroundColor','white');end% --- Executes on key press with focus on pushbutton3 and none of its controls.function pushbutton3_KeyPressFcn(hObject, eventdata, handles)% hObject    handle to pushbutton3 (see GCBO)% eventdata  structure with the following fields (see UICONTROL)%	Key: name of the key that was pressed, in lower case%	Character: character interpretation of the key(s) that was pressed%	Modifier: name(s) of the modifier key(s) (i.e., control, shift) pressed% handles    structure with handles and user data (see GUIDATA)function uipanel2_SelectionChangeFcn(hObject, eventdata)%retrieve GUI data, i.e. the handles structurehandles = guidata(hObject);switch get(eventdata.NewValue,'Tag') % Get Tag of selected object    case 'radiobutton1'        handles.RadioButtonNumber = 1;    case 'radiobutton2'        handles.RadioButtonNumber = 2;end %updates the handles structureguidata(hObject, handles);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -