matlab快速傅立叶变换小程序(含GUI)

function varargout = Experiment1(varargin)
% EXPERIMENT1 M-file for Experiment1.fig
%      EXPERIMENT1, by itself, creates a new EXPERIMENT1 or raises the existing
%      singleton*.
%
%      H = EXPERIMENT1 returns the handle to a new EXPERIMENT1 or the handle to
%      the existing singleton*.
%
%      EXPERIMENT1('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in EXPERIMENT1.M with the given input arguments.
%
%      EXPERIMENT1('Property','Value',...) creates a new EXPERIMENT1 or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before Experiment1_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to Experiment1_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 Experiment1
 
% Last Modified by GUIDE v2.5 06-Oct-2012 15:04:02
 
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @Experiment1_OpeningFcn, ...
                   'gui_OutputFcn',  @Experiment1_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 Experiment1 is made visible.
function Experiment1_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 Experiment1 (see VARARGIN)
 
% Choose default command line output for Experiment1
handles.output = hObject;
 
% Update handles structure
guidata(hObject, handles);
 
% UIWAIT makes Experiment1 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
 
 
% --- Outputs from this function are returned to the command line.
function varargout = Experiment1_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;
 
 
 
% 图形标题编辑框
function eTitleInput_Callback(hObject, eventdata, handles)
% hObject    handle to eTitleInput (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 eTitleInput as text
%        str2double(get(hObject,'String')) returns contents of eTitleInput as a double
 
 
% --- Executes during object creation, after setting all properties.
function eTitleInput_CreateFcn(hObject, eventdata, handles)
% hObject    handle to eTitleInput (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 button press in pAddTitle.
function pAddTitle_Callback(hObject, eventdata, handles)
% hObject    handle to pAddTitle (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
title(get(findobj(gcf, 'Tag', 'eTitleInput'), 'String'));
 
 
 
% 关闭窗口按钮
% --- Executes on button press in pCloseGUI.
function pCloseGUI_Callback(hObject, eventdata, handles)
% hObject    handle to pCloseGUI (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
close;
 
 
 
% 打开网格复选框
% --- Executes on button press in cheOpenGrid.
function cheOpenGrid_Callback(hObject, eventdata, handles)
% hObject    handle to cheOpenGrid (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 cheOpenGrid
Fig_UserData = get(gcf, 'UserData');
if get(gcbo, 'Value') == 1
    grid on
else
    grid off
end;
Fig_UserData(1) = get(gcbo, 'Value');
set(gcf, 'UserData', Fig_UserData);
 
 
 
% 显示坐标轴复选框
% --- Executes on button press in cheShowAxis.
function cheShowAxis_Callback(hObject, eventdata, handles)
% hObject    handle to cheShowAxis (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 cheShowAxis
Fig_UserData = get(gcf, 'UserData');
if get(gcbo, 'Value') == 1
    axis on
else
    axis off
end;
Fig_UserData(2) = get(gcbo, 'Value');
set(gcf, 'UserData', Fig_UserData);
 
 
 
% Hold on复选框
% --- Executes on button press in cheHoldOn.
function cheHoldOn_Callback(hObject, eventdata, handles)
% hObject    handle to cheHoldOn (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 cheHoldOn
Fig_UserData = get(gcf, 'UserData');
if get(gcbo, 'Value') == 1
    hold on;
else
    hold off;
end;
Fig_UserData(3) = get(gcbo, 'Value');
set(gcf, 'UserData', Fig_UserData);
 
 
 
% 是否补零的按钮
% --- Executes on button press in radIfZeros.
function radIfZeros_Callback(hObject, eventdata, handles)
% hObject    handle to radIfZeros (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 radIfZeros
Fig_UserData = get(gcf, 'UserData');
Fig_UserData(4) = get(gcbo, 'Value');
set(gcf, 'UserData', Fig_UserData);
 
 
% 信号频率输入函数
function eFrequency_Callback(hObject, eventdata, handles)
% hObject    handle to eFrequency (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 eFrequency as text
%        str2double(get(hObject,'String')) returns contents of eFrequency as a double
 
 
% --- Executes during object creation, after setting all properties.
function eFrequency_CreateFcn(hObject, eventdata, handles)
% hObject    handle to eFrequency (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
%set(hObject, 'String', '25');
 
 
 
% 采样点数输入函数
function ePoint_Callback(hObject, eventdata, handles)
% hObject    handle to ePoint (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 ePoint as text
%        str2double(get(hObject,'String')) returns contents of ePoint as a double
 
 
% --- Executes during object creation, after setting all properties.
function ePoint_CreateFcn(hObject, eventdata, handles)
% hObject    handle to ePoint (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
 
 
 
% 时间间隔输入函数
function eTime_Callback(hObject, eventdata, handles)
% hObject    handle to eTime (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 eTime as text
%        str2double(get(hObject,'String')) returns contents of eTime as a double
 
 
% --- Executes during object creation, after setting all properties.
function eTime_CreateFcn(hObject, eventdata, handles)
% hObject    handle to eTime (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 selection change in pLineShape.
function pLineShape_Callback(hObject, eventdata, handles)
% hObject    handle to pLineShape (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
 
% Hints: contents = cellstr(get(hObject,'String')) returns pLineShape contents as cell array
%        contents{get(hObject,'Value')} returns selected item from pLineShape
num = get(gcbo, 'Value');
Fig_UserData = get(gcf, 'UserData');
Fig_UserData(5) = num;
set(gcf, 'UserData', Fig_UserData);
%isHold = get(findobj(gcf, 'Tag', 'cheHoldOn'), 'Value');
%Graphic_Draw(Fig_UserData, isHold);
 
 
% --- Executes during object creation, after setting all properties.
function pLineShape_CreateFcn(hObject, eventdata, handles)
% hObject    handle to pLineShape (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
 
% Hint: popupmenu 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 selection change in pPointShape.
function pPointShape_Callback(hObject, eventdata, handles)
% hObject    handle to pPointShape (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
 
% Hints: contents = cellstr(get(hObject,'String')) returns pPointShape contents as cell array
%        contents{get(hObject,'Value')} returns selected item from pPointShape
num = get(gcbo, 'Value');
Fig_UserData = get(gcf, 'UserData');
Fig_UserData(6) = num;
set(gcf, 'UserData', Fig_UserData);
%isHold = get(findobj(gcf, 'Tag', 'cheHoldOn'), 'Value');
%Graphic_Draw(Fig_UserData, isHold);
 
 
% --- Executes during object creation, after setting all properties.
function pPointShape_CreateFcn(hObject, eventdata, handles)
% hObject    handle to pPointShape (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
 
% Hint: popupmenu 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 selection change in pColor.
function pColor_Callback(hObject, eventdata, handles)
% hObject    handle to pColor (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
 
% Hints: contents = cellstr(get(hObject,'String')) returns pColor contents as cell array
%        contents{get(hObject,'Value')} returns selected item from pColor
num = get(gcbo, 'Value');
Fig_UserData = get(gcf, 'UserData');
Fig_UserData(7) = num;
set(gcf, 'UserData', Fig_UserData);
%isHold = get(findobj(gcf, 'Tag', 'cheHoldOn'), 'Value');
%Graphic_Draw(Fig_UserData, isHold);
 
 
% --- Executes during object creation, after setting all properties.
function pColor_CreateFcn(hObject, eventdata, handles)
% hObject    handle to pColor (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
 
% Hint: popupmenu 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
 
 
 
%创建一个函数对应绘图函数实现的功能
function Graphic_Draw(Fig_UserData, isHold, isAutoFFT)
%Fig_UserData为输入参数,对应于Figure的属性'UserData'的值
%isHold判断hold on或hold off
 
clc
% 检查参量长度
if(length(Fig_UserData) ~= 7)
    errordlg('输入参量不符合长度要求');
    return;
end
 
 
% 判断线型
switch Fig_UserData(5)
    case 1
        strLineType = '-'; % 实线
    case 2
        strLineType = '--'; % 双划线
    case 3
        strLineType = ':'; % 虚线
    case 4
        strLineType = '-.'; % 点划线
    otherwise
        strLineType = '-'; % 其他为实线
end
 
% 判断点型
switch Fig_UserData(6)
    case 1
        strPointType = ''; % 无点型
    case 2  
        strPointType = '+'; % 十字符
    case 3  
        strPointType = 'o'; % 空心圆
    case 4  
        strPointType = '*'; % 星号
    case 5  
        strPointType = '.'; % 实心圆
    case 6  
        strPointType = 'x'; % 叉符
    case 7  
        strPointType = 's'; % 正方符
    case 8  
        strPointType = 'd'; % 菱形符
    case 9  
        strPointType = '^'; % 上三角符
    case 10 
        strPointType = 'v'; % 下三角符
    case 11 
        strPointType = '<'; % 左三角符
    case 12 
        strPointType = '>'; % 右三角符
    case 13 
        strPointType = 'p'; % 五星符
    case 14 
        strPointType = 'h'; % 六星符
    otherwise 
        strPointType = '';
end
 
% 判断颜色
switch Fig_UserData(7)
    case 1 
        strColor = 'k'; % 黑色
    case 2 
        strColor = 'r'; % 红色
    case 3 
        strColor = 'g'; % 绿色
    case 4 
        strColor = 'b'; % 蓝色
    case 5 
        strColor = 'c'; % 青色
    case 6 
        strColor = 'm'; % 品红色
    case 7 
        strColor = 'y'; % 黄色
    case 8 
        strColor = 'w'; % 白色
    otherwise 
        strColor = 'k';
end
 
% 连接成一个字符串
strType = strcat(strLineType, strPointType, strColor);
 
% 为Freq设置初值
Freq = 50;
% 得到输入的频率
Freq = str2num(get(findobj(gcf, 'Tag', 'eFrequency'), 'String'));
% 是否采用默认频率
if (Freq == 0)
    Freq = 50;
end
 
% 为PointNum设置初值
PointNum = 32;
% 得到输入的采样点数
PointNum = str2num(get(findobj(gcf, 'Tag', 'ePoint'), 'String'));
% 是否采用默认采样点数
if (PointNum == 0)
    pointNum = 32;
end
if ((PointNum ~= 32) && (PointNum ~= 64))
    errordlg('采样点数不是32或64。');
    return;
end
 
% 为TimeDis设置初值
TimeDis = 0.000625;
% 得到输入的采样间隔
TimeDis = str2num(get(findobj(gcf, 'Tag', 'eTime'), 'String'));
% 是否采用默认采样间隔
if (TimeDis == 0)
    TimeDis = 0.000625;
end
 
% 获得时域的采样值
t = 0:TimeDis:TimeDis * (PointNum - 1);
ft = sin(2 * pi * Freq * t);
 
% 判断是否补零
if ((Freq == 50) && (PointNum == 32) && (TimeDis == 0.0046875) && (Fig_UserData(4) == 1))
    ft = [ft, zeros(1, 32)];
end
k = 0:1:(length(ft) - 1);
     
disp('X(k)的值:')
% FFT计算
if (isAutoFFT == 1)
    % 使用matlab函数计算fft
    FFTOut = abs(fft(ft));
    yyy = fft(ft);
    disp(yyy);
elseif (isAutoFFT == 0)
    % 自己编制函数计算fft
    FFTOut = myFFT(ft);
else
    errordlg('参数传输错误!');
end
 
% 归一化
maxFFTOut = max(FFTOut);
FFTOut = FFTOut ./ maxFFTOut;
 
% 画图
stem(k, FFTOut, strType);
 
% 坐标轴设置
if Fig_UserData(2)
    axis on
else
    axis off
end
xlabel('\itk');
ylabel('模特性|\itX(k)\rm|');
 
% 网格设置
if Fig_UserData(1)
    grid on
else
    grid off
end
 
 
% 使用matlab的fft函数绘图
% --- Executes on button press in pAutoDrawFig.
function pAutoDrawFig_Callback(hObject, eventdata, handles)
% hObject    handle to pAutoDrawFig (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
Fig_UserData = [get(findobj(gcf, 'Tag', 'cheOpenGrid'), 'Value'),...
    get(findobj(gcf, 'Tag', 'cheShowAxis'), 'Value'),...
    get(findobj(gcf, 'Tag', 'cheHoldOn'), 'Value'),...
    get(findobj(gcf, 'Tag', 'radIfZeros'), 'Value'),...
    get(findobj(gcf, 'Tag', 'pLineShape'), 'Value'),...
    get(findobj(gcf, 'Tag', 'pPointShape'), 'Value'),...
    get(findobj(gcf, 'Tag', 'pColor'), 'Value')];
isHold = get(findobj(gcf, 'Tag', 'cheHoldOn'), 'Value');
isAutoFFT = 1;
Graphic_Draw(Fig_UserData, isHold, isAutoFFT);
 
 
% 使用用户定义的函数绘图
% --- Executes on button press in pUserDrawFig.
function pUserDrawFig_Callback(hObject, eventdata, handles)
% hObject    handle to pUserDrawFig (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
Fig_UserData = [get(findobj(gcf, 'Tag', 'cheOpenGrid'), 'Value'),...
    get(findobj(gcf, 'Tag', 'cheShowAxis'), 'Value'),...
    get(findobj(gcf, 'Tag', 'cheHoldOn'), 'Value'),...
    get(findobj(gcf, 'Tag', 'radIfZeros'), 'Value'),...
    get(findobj(gcf, 'Tag', 'pLineShape'), 'Value'),...
    get(findobj(gcf, 'Tag', 'pPointShape'), 'Value'),...
    get(findobj(gcf, 'Tag', 'pColor'), 'Value')];
isHold = get(findobj(gcf, 'Tag', 'cheHoldOn'), 'Value');
isAutoFFT = 0;
Graphic_Draw(Fig_UserData, isHold, isAutoFFT);
 
 
% 用户自定义的FFT函数
function absFFTOut = myFFT(ft)
% 码位倒置变址
inputLength = length(ft);
inputLog = log2(inputLength);
tempInput = zeros(1,inputLength);
for i = 1:1:inputLength
    tempbii = de2bi(i - 1, inputLog); % 将下标转换为2进制
    tempdei = 1;
    % 将下标倒置后转换为十进制
    for ii = 1:1:inputLog
        tempdei = tempdei + tempbii(ii) * (2 ^ (inputLog - ii));    
    end
    tempInput(tempdei) = ft(i); % 将倒置后的值赋给临时变量
end
% 时间抽取FFT
tempOut = zeros(1, inputLength);
for ii = 1:1:inputLog % 第ii层蝶形运算
    for i = 1:(2^ii):inputLength % 第i组
        iiii = -1;
        for iii = i:1:(i + 2^(ii - 1) - 1) % 组内计算
            iiii = iiii + 1;
            WNk = exp(-1i * 2 * pi / inputLength * iiii*(2^(inputLog - ii)));
            tempOut(iii) = tempInput(iii) + WNk * tempInput(iii + 2^(ii - 1));
            tempOut(iii + 2^(ii - 1)) = tempInput(iii) - WNk * tempInput(iii + 2^(ii - 1));
        end
    end
    tempInput = tempOut;
end
absFFTOut = abs(tempOut);
 
 
% 绘图说明
% --- Executes on button press in pExplain.
function pExplain_Callback(hObject, eventdata, handles)
% hObject    handle to pExplain (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
msgbox('1.当参数输入为0或无输入时,默认设置为a)信号频率f=50Hz,采样点数N=32,采样间隔T=0.000625s。  2.补0按钮只在c)信号频率f=50Hz,采样点数N=32,采样间隔T=0.0046875s时有效。','绘图说明','modal');

matlab快速傅立叶变换小程序(含GUI)_第1张图片

你可能感兴趣的:(matlab快速傅立叶变换小程序(含GUI))