1. 程式人生 > 其它 >【數字訊號處理】基於matlab GUI頻譜分析儀【含Matlab原始碼 932期】

【數字訊號處理】基於matlab GUI頻譜分析儀【含Matlab原始碼 932期】

一、簡介

基於matlab GUI頻譜分析儀

二、原始碼

unction varargout = ch4(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @ch4_OpeningFcn, ...
                   'gui_OutputFcn',  @ch4_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 ch4 is made visible.
function ch4_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 ch4 (see VARARGIN)

% Choose default command line output for ch4
handles.inputtype=0;
xlabel(handles.plot1,'freqency(Hz)');
xlabel(handles.plot2,'freqency(Hz)');
xlabel(handles.plot3,'freqency(Hz)');
xlabel(handles.plot4,'freqency(Hz)');
xlabel(handles.plot5,'freqency(Hz)');
ylabel(handles.plot1,'amplitude');
ylabel(handles.plot2,'phase(rad)');
ylabel(handles.plot3,'real');
ylabel(handles.plot4,'Imaginary');
ylabel(handles.plot5,'power');

handles.output = hObject;

% Update handles structure
guidata(hObject, handles);            %更新資料

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


% --- Outputs from this function are returned to the command line.
function varargout = ch4_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 during object creation, after setting all properties.
function figure1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% --- Executes on button press in timeanalyse.
function timeanalyse_Callback(hObject, eventdata, handles)            %時域分析Callback
% hObject    handle to timeanalyse (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
Fs=str2double(get(findobj('Tag','samplerate'),'String'));      %獲得介面取樣頻率數值
N=str2double(get(findobj('Tag','samplenum'),'String'));        %獲得介面取樣點數數值
if handles.inputtype==0                                        %handles.inputtype記錄輸入方式  0(預設)為當前無輸入;1為音效卡錄音方式;2為開啟檔案方式;3為訊號生成方式
        msgbox('No wave exist! Please choose a input type!');
        return;
end
%過 零 監 測
n=1;
ymax=max([handles.y(1) handles.y(2)]);
ymin=min([handles.y(1) handles.y(2)]);
from=str2double(get(handles.pointfrom,'String'));
to=str2double(get(handles.pointto,'String'));
if from<1 | to-from<5;            %計算區間過小判定
    msgbox('Error range!');
    return;
end
for i=from+2:to-1;
    if handles.y(i-1)<0 & handles.y(i-2)<0 & handles.y(i)>=0 & handles.y(i+1)>0
        if handles.y(i)==0             %若y(i)為0,直接得到零點序號
            ti(n)=i;
        else
            ti(n)=i-handles.y(i)/(handles.y(i)-handles.y(i-1));   %線性內插得到零點序號
%             x1=i-1;
%             y1=handles.y(i-1);
%             x2=i;
%             y2=handles.y(i);
%             a=handles.y(i)-handles.y(i-1);
%             b=a*x1-y1;
%             ti(n)=b/a;
        end
        amp(n)=(ymax-ymin)/2;
        ymax=0;
        ymin=0;
        n=n+1;
    else
        if ymax<handles.y(i)
            ymax=handles.y(i);
        end
        if ymin>handles.y(i)
            ymin=handles.y(i);
        end
    end
end
n=n-1;
%freqence and periodicity
for i=1:n-1
    T(i)=ti(i+1)-ti(i);
end
freq=Fs/mean(T);
set(handles.outt,'String',1/freq);                                                   %返回訊號週期估計
set(handles.outfreq,'String',num2str(freq));                                         %返回訊號頻率估計
%amplitude
set(handles.outamp,'String',num2str(mean(amp(2:n-1))));                              %返回訊號幅值估計
%phase
phase=2*pi*(1-(ti(1:n-1)-1)./T+floor((ti(1:n-1)-1)./T));
set(handles.outphase,'String',num2str(mean(phase)));                                 %返回訊號相位估計
%peak
set(handles.outpeak,'String',(max(handles.y(from:to))-min(handles.y(from:to)))/2);   %返回訊號峰值估計
%mean
set(handles.outmean,'String',mean(handles.y(from:to)));                              %返回訊號均值估計
%meansquare
set(handles.outmeansquare,'String',mean(handles.y(from:to).^2));                     %返回訊號均方差估計
%s
set(handles.outs,'String',(std(handles.y(from:to)))^2);                              %返回訊號方差估計


function filename_Callback(hObject, eventdata, handles)
% hObject    handle to filename (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 filename as text
%        str2double(get(hObject,'String')) returns contents of filename as a double


% --- Executes during object creation, after setting all properties.
function filename_CreateFcn(hObject, eventdata, handles)
% hObject    handle to filename (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function freq_Callback(hObject, eventdata, handles)
% hObject    handle to freq (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 freq as text
%        str2double(get(hObject,'String')) returns contents of freq as a double


% --- Executes during object creation, after setting all properties.
function freq_CreateFcn(hObject, eventdata, handles)
% hObject    handle to freq (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end

三、執行結果

四、備註

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