1. 程式人生 > 其它 >【語音處理】基於matlab GUI語音時域頻域頻譜圖分析【含Matlab原始碼 527期】

【語音處理】基於matlab GUI語音時域頻域頻譜圖分析【含Matlab原始碼 527期】

一、簡介

時域和頻域是訊號的基本性質,這樣可以用多種方式來分析訊號,每種方式提供了不同的角度。解決問題的最快方式不一定是最明顯的方式,用來分析訊號的不同角度稱為域。時域頻域可清楚反應訊號與互連線之間的相互影響。
1 時域
時域是真實世界,是惟一實際存在的域。因為我們的經歷都是在時域中發展和驗證的,已經習慣於事件按時間的先後順序地發生。而評估數字產品的效能時,通常在時域中進行分析,因為產品的效能最終就是在時域中測量的。

時鐘波形的兩個重要引數是時鐘週期和上升時間。

圖中標明瞭1GHz時鐘訊號的時鐘週期和10-90上升時間。下降時間一般要比上升時間短一些,有時會出現更多的噪聲。

時鐘週期就是時鐘迴圈重複一次的時間間隔,通常用ns度量。時鐘頻率Fclock,即1秒鐘內時鐘迴圈的次數,是時鐘週期Tclock的倒數。

Fclock=1/Tclock

上升時間與訊號從低電平跳變到高電平所經歷的時間有關,通常有兩種定義。一種是10-90上升時間,指訊號從終值的10%跳變到90%所經歷的時間。這通常是一種預設的表達方式,可以從波形的時域圖上直接讀出。第二種定義方式是20-80上升時間,這是指從終值的20%跳變到80%所經歷的時間。

時域波形的下降時間也有一個相應的值。根據邏輯系列可知,下降時間通常要比上升時間短一些,這是由典型CMOS輸出驅動器的設計造成的。在典型的輸出驅動器中,p管和n管在電源軌道Vcc和Vss間是串聯的,輸出連在這個兩個管子的中間。在任一時間,只有一個電晶體導通,至於是哪一個管子導通取決於輸出的高或低狀態。

2 頻域
頻域,尤其在射頻和通訊系統中運用較多,在高速數字應用中也會遇到頻域。頻域最重要的性質是:它不是真實的,而是一個數學構造。時域是惟一客觀存在的域,而頻域是一個遵循特定規則的數學範疇,頻域也被一些學者稱為上帝視角。

正弦波是頻域中唯一存在的波形,這是頻域中最重要的規則,即正弦波是對頻域的描述,因為頻域中的任何波形都可用正弦波合成。這是正弦波的一個非常重要的性質。然而,它並不是正弦波的獨有特性,還有許多其他的波形也有這樣的性質。正弦波有四個性質使它可以有效地描述其他任一波形:

(1)頻域中的任何波形都可以由正弦波的組合完全且惟一地描述。

(2)任何兩個頻率不同的正弦波都是正交的。如果將兩個正弦波相乘並在整個時間軸上求積分,則積分值為零。這說明可以將不同的頻率分量相互分離開。

(3)正弦波有精確的數學定義。

(4)正弦波及其微分值處處存在,沒有上下邊界。

使用正弦波作為頻域中的函式形式有它特別的地方。若使用正弦波,則與互連線的電氣效應相關的一些問題將變得更容易理解和解決。如果變換到頻域並使用正弦波描述,有時會比僅僅在時域中能更快地得到答案。

而在實際中,首先建立包含電阻,電感和電容的電路,並輸入任意波形。一般情況下,就會得到一個類似正弦波的波形。而且,用幾個正弦波的組合就能很容易地描述這些波形,如下圖2.2所示:

圖2.2 理想RLC電路相互作用的時域行為

3 時域頻域的關係
時域分析與頻域分析是對模擬訊號的兩個觀察面。時域分析是以時間軸為座標表示動態訊號的關係;頻域分析是把訊號變為以頻率軸為座標表示出來。一般來說,時域的表示較為形象與直觀,頻域分析則更為簡練,剖析問題更為深刻和方便。訊號分析的趨勢是從時域向頻域發展。然而,它們是互相聯絡,缺一不可,相輔相成的。
4 時域和頻域的轉換
動態訊號從時間域變換到頻率域主要通過傅立葉級數和傅立葉變換實現。週期訊號靠傅立葉級數,非週期訊號靠傅立葉變換。時域越寬,頻域越短。

s(f) = ∫-∞ +∞ (s(t)·e)dt

sD(t)= dS(t)/dt

sD(f)= ∫-∞ (sD(t)·e-j2∏ft)dt=j·2∏f· s(f)

二、原始碼

function varargout = gui(varargin)
% GUI MATLAB code for gui.fig
%      GUI, by itself, creates a new GUI or raises the existing
%      singleton*.
%
%      H = GUI returns the handle to a new GUI or the handle to
%      the existing singleton*.
%
%      GUI('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in GUI.M with the given input arguments.
%
%      GUI('Property','Value',...) creates a new GUI or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before gui_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to gui_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 gui

% Last Modified by GUIDE v2.5 23-Apr-2019 15:32:28

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @gui_OpeningFcn, ...
                   'gui_OutputFcn',  @gui_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 gui is made visible.
function gui_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 gui (see VARARGIN)

% Choose default command line output for gui
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes gui wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = gui_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 structure
varargout{1} = handles.output;


% --- 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)
global aud RecordLength ShowLength FrequencyWindow1 FrequencyWindow2 fs nBits
global mag idx_last recObj myRecording bit




record(recObj,RecordLength);
tic
while toc<.1
end
bit = 2;
while toc<RecordLength
    myRecording = getaudiodata(recObj);
    idx = round(toc*fs);
    while idx-idx_last<.1*fs
       idx=round(toc*fs);
    end
   axes(handles.axes1)
   plot((max(1,size(myRecording,1)-fs*ShowLength):(2^bit):size(myRecording,1))./fs,myRecording(max(1,size(myRecording,1)-fs*ShowLength):(2^bit):end))
   mag = max(abs(myRecording));
   ylim([-1.2 1.2]*mag)
   xlim([max(0,size(myRecording,1)/fs-ShowLength)
   max(size(myRecording,1)/fs,ShowLength)])
   title('聲音訊號的波形顯示');
   ylabel('Signal level(volts)');
   xlabel('Samples');
   drawnow
   idx_last = idx;
end
% --- 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)
global aud RecordLength ShowLength FrequencyWindow1 FrequencyWindow2 fs nBits
global mag idx_last recObj myRecording
save data.mat aud RecordLength ShowLength FrequencyWindow1 FrequencyWindow2 fs nBits mag idx_last recObj myRecording 

% --- 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)


% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
clc,clear,
close all force;

% --- Executes on button press in pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global aud RecordLength ShowLength FrequencyWindow1 FrequencyWindow2 fs nBits
global mag idx_last recObj
aud = audiodevinfo;
RecordLength = 15;
ShowLength = 15;
FrequencyWindow1 = 0;
FrequencyWindow2 = 3000;
fs = 44100;
nBits = 16;
mag = 1.05;
idx_last = 1;
 recObj = audiorecorder(fs,nBits,1);
 if isempty(recObj);
     
 else
     msgbox('語音裝置已檢測到!!!');
 end

% --- Executes on button press in pushbutton6.
function pushbutton6_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on button press in pushbutton7.
function pushbutton7_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton7 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global fs
load('data.mat')
sound(myRecording,fs);


% --------------------------------------------------------------------
function Untitled_1_Callback(hObject, eventdata, handles)
% hObject    handle to Untitled_1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global aud RecordLength ShowLength FrequencyWindow1 FrequencyWindow2 fs nBits
global mag idx_last recObj myRecording bit x3
x1 = myRecording(max(1,size(myRecording,1)-fs*ShowLength):(2^bit):end);
N=length(x1);
x2 = x1-mean(x1);
x3 = smooth(x2);
axes(handles.axes1)
plot(x3,'b-','linewidth',2)
axis tight
title('零均值化——平滑濾波')

% --------------------------------------------------------------------
function Untitled_2_Callback(hObject, eventdata, handles)
% hObject    handle to Untitled_2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

三、執行結果


四、備註

版本:2014a
完整程式碼或代寫加1564658423