题目分析:
本题要求设计一个基于MATLAB的图形用户界面(GUI),用于实现任意时域信号的频谱分析,特别是利用离散傅里叶变换(DFT)或快速傅里叶变换(FFT)来处理信号。题目涉及了数字信号处理中的几个关键知识点,包括信号的数字化、采样定理、DFT/FFT的基本原理及其在频谱分析中的应用。
DFT是离散傅里叶变换,其实质是有限长序列傅里叶变换的有限点离散采样,从而实现了频域离散化,使数字信号处理可以在频域采用数值运算的方法进行,这样就大大增加了数字信号处理的灵活性。并且,DFT有多种快速算法,统称为快速傅里叶变换(Fast Fourier Transform,FFT),从而使信号的实时处理和设备的简化得以实现。
对于本题来说,首先根据输入信号的种类不同执行不同的操作,数字信号:用户可以直接在GUI中输入一系列数字值,这些值代表时域信号的样本。
模拟信号:对于模拟信号,用户需要通过函数表达式的方式描述信号,然后根据用户输入的采样频率Fs实现对该信号的采样。这涉及到采样定理,即采样频率必须大于或等于信号中最高频率的两倍,以避免混叠现象。
既然要求能在GUI中输入时域数字或模拟信号,所以应该设定一个下拉选择框,以供选择是模拟信号还是数字信号,而若是模拟信号,则应再加一个可编辑文本框来供用户来设定采样频率Fs。若是数字信号,则用stem来画图,若是模拟信号,则用plot来画图
输入信号经过DFT/FFT处理后,信号从时域转换到频域,得到的是信号的频谱。频谱图通常包括幅度谱和相位谱,但在此题目中,主要关注的是幅度谱。
设计思路:
创建两个轴,一个用于显示原函数的图像,一个用于显示进行傅里叶变换后的图像,设置一个可编辑文本框用于输入原函数,设置一个内容为“数字信号”和“模拟信号”的列表框,设置一个“开始变换”按钮,本题主要的matlab程序都写在此按钮的回调函数中,接着设置用于存放dft点数的可编辑文本框以及用于存放采样频率的可编辑文本框,整体的布局如下:
接着开始在“开始变换”按钮中写回调函数,运用eval(“x=”+get(handles.edit1,‘String’))取出edit1中的原函数,然后若原函数是数字信号,先if((isempty(get(handles.edit5,'String'))))判断一下edit5中是否有值,若有值,则把它赋给N,N即为dft点数。原函数是数字信号,则在画原函数的图时用stem来画图,而在画傅里叶变换后的图时,先求出采样间隔,接着令F=fft(x,N)此时F即为dft点数为N的x的傅里叶变换,此时,令frequencies = 0:Fs/length(F):Fs-Fs/length(F);即可用plot(frequencies,abs(F))来画出数字信号傅里叶变换后的图像了。若原函数是模拟信号,则根据用户设定的Fs求出采样间隔,T=1/Fs,求出持续时间t,把t代入x中后,对x进行fft(x),最后用plot(Fs/L*(0:L-1),abs(F),"LineWidth",3)来求出模拟信号傅里叶变换后的图,其中L是波形持续时间,这里设定为1500.
结果如图:
代码
function varargout = untitled(varargin)
% UNTITLED MATLAB code for untitled.fig
% UNTITLED, by itself, creates a new UNTITLED or raises the existing
% singleton*.
%
% H = UNTITLED returns the handle to a new UNTITLED or the handle to
% the existing singleton*.
%
% UNTITLED('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in UNTITLED.M with the given input arguments.
%
% UNTITLED('Property','Value',...) creates a new UNTITLED or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before untitled_OpeningFcn gets called. An
% unrecognized property name or invali property application
% stop. All inputs are passed to untitled_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 untitled
% Last Modified by GUIDE v2.5 08-Jun-2024 23:04:49
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @untitled_OpeningFcn, ...
'gui_OutputFcn', @untitled_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 untitled is made visible.
function untitled_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 untitled (see VARARGIN)
% Choose default command line output for untitled
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes untitled wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = untitled_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 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');
end
% --- Executes during object creation, after setting all properties.
function axes2_CreateFcn(hObject, eventdata, handles)
% hObject handle to axes2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: place code in OpeningFcn to populate axes2
% --- 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)
axes(handles.axes1);
cla;
axes(handles.axes2);
cla;
axes(handles.axes1);
hold on;
if(~(isempty(get(handles.edit7,'String'))))
eval("n="+get(handles.edit7,'String'))
Fs = 1 / (n(2) - n(1))
else
end
if(~(isempty(get(handles.edit8,'String'))))
eval("t="+get(handles.edit8,'String'));
Fs = 1 / (t(2) - t(1))
else
end
if(~(isempty(get(handles.edit6,'String'))))
% ²ÉÑùƵÂÊ
eval("Fs="+get(handles.edit6,'String'))
end
T = 1/Fs; % Sampling period
t = -0.5:T:0.5; % Time vector
L = length(t);
switch (get(handles.popupmenu3,'Value'))%pd
case 1
eval("x=["+get(handles.edit1,'String')+"];");
if((isempty(get(handles.edit5,'String'))))
N=length(x);
else
eval("N="+get(handles.edit5,'String'));
end
case 2
end
switch (get(handles.popupmenu3,'Value'))%pd
case 1
stem(n,x);
case 2
T=1/Fs
L = 1500; % Length of signal
t = (0:L-1)*T;
eval("x=["+get(handles.edit1,'String')+"]");
plot(1000*t,x)
end
axes(handles.axes2);
switch (get(handles.popupmenu3,'Value'))%pd
case 1
T=0:1:length(x)-1;
y=x;
F=fft(y,N);
frequencies = 0:Fs/length(F):Fs-Fs/length(F);
plot(frequencies,abs(F));
case 2
T=1/Fs
L = 1500; % Length of signal
t = (0:L-1)*T;
eval("x="+get(handles.edit1,'String'));
F=fft(x);
plot(Fs/L*(0:L-1),abs(F),"LineWidth",3)
end
function 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 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)
axes(handles.axes1);
cla;
axes(handles.axes2);
cla;
eval(get(handles.edit2,'String'));
axes(handles.axes1);
if get(handles.radiobutton1,'Value')
eval(get(handles.edit1,'String'));
y=x+randn(size(t));
plot(t,y);
axes(handles.axes2);
F=fft(y);
Fs = 1 / (t(2) - t(1)); % ²ÉÑùƵÂÊ
frequencies = 0:Fs/length(F):Fs-Fs/length(F);
plot(frequencies,abs(F));
if ~(isempty(get(handles.edit3))||isempty(get(handles.edit4)))
axes(handles.axes1);
hold on;
t=str2num(get(handles.edit3,'String'));
p=strcat('\leftarrow ', get(handles.edit4,'String'))
eval(get(handles.edit1,'String'));
y=x+randn(size(t));
text(t,y,p,'color','red');
plot(t,y,'rx');
axes(handles.axes2);
df=Fs/16;
hold on;
f=2*pi*t;
k = round(f / df) + 1;
plot(f,F(k),'r*');
p=strcat('\leftarrow ', get(handles.edit4,'String'))
text(f,F(k),p,'color','red');
end
else
axes(handles.axes1);
cla;
axes(handles.axes2);
cla;
axes(handles.axes1);
eval(get(handles.edit1,'String'));
plot(t,x);
axes(handles.axes2);
F=fft(x);
Fs = 1 / (t(2) - t(1));
frequencies = 0:Fs/length(F):Fs-Fs/length(F);
plot(frequencies,abs(F));
end
% Hint: get(hObject,'Value') returns toggle state of radiobutton1
% --- Executes on button press in pushbutton2.
% --- Executes on selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
b=get(handles.popupmenu1,'Value');
line1 = findobj(handles.axes1, 'Type', 'line');
line2 = findobj(handles.axes2, 'Type', 'line');
switch b
case 2
set(line1, 'Color', 'r');
set(line2, 'Color', 'r');
case 3
set(line1, 'Color', 'm');
set(line2, 'Color', 'm');
case 4
set(line1, 'Color', 'g');
set(line2, 'Color', 'g');
case 5
set(line1, 'Color', 'c');
set(line2, 'Color', 'c');
case 6
set(line1, 'Color', 'b');
set(line2, 'Color', 'b');
end
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu1 contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu1
% --- Executes during object creation, after setting all properties.
function popupmenu1_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenu1 (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 popupmenu2.
function popupmenu2_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
c=get(handles.popupmenu2,'Value');
sty1 = findobj(handles.axes1, 'Type', 'line');
sty2 = findobj(handles.axes2, 'Type', 'line');
switch c
case 2
set(sty1, 'LineStyle', '-');
set(sty2, 'LineStyle', '-');
case 3
set(sty1, 'LineStyle', ':');
set(sty2, 'LineStyle', ':');
case 4
set(sty1, 'LineStyle', '-.');
set(sty2, 'LineStyle', '-.');
case 5
set(sty1, 'LineStyle', '--');
set(sty2, 'LineStyle', '--');
end
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu2 contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu2
% --- Executes during object creation, after setting all properties.
function popupmenu2_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenu2 (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 edit3_Callback(hObject, eventdata, handles)
% hObject handle to edit3 (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 edit3 as text
% str2double(get(hObject,'String')) returns contents of edit3 as a double
% --- Executes during object creation, after setting all properties.
function edit3_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit3 (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 edit4_Callback(hObject, eventdata, handles)
% hObject handle to edit4 (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 edit4 as text
% str2double(get(hObject,'String')) returns contents of edit4 as a double
% --- Executes during object creation, after setting all properties.
function edit4_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit4 (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 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)
axes(handles.axes1);
cla;
axes(handles.axes2);
cla;
% --------------------------------------------------------------------
function Untitled_3_Callback(hObject, eventdata, handles)
% hObject handle to Untitled_3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
grid on;
% --------------------------------------------------------------------
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)
% --------------------------------------------------------------------
function Untitled_4_Callback(hObject, eventdata, handles)
% hObject handle to Untitled_4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
grid off;
% --- Executes during object creation, after setting all properties.
function text9_CreateFcn(hObject, eventdata, handles)
% hObject handle to text9 (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 selection change in popupmenu3.
function popupmenu3_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu3 (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 popupmenu3 contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu3
% --- Executes during object creation, after setting all properties.
function popupmenu3_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenu3 (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 edit5_Callback(hObject, eventdata, handles)
% hObject handle to edit5 (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 edit5 as text
% str2double(get(hObject,'String')) returns contents of edit5 as a double
% --- Executes during object creation, after setting all properties.
function edit5_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit5 (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 edit6_Callback(hObject, eventdata, handles)
% hObject handle to edit6 (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 edit6 as text
% str2double(get(hObject,'String')) returns contents of edit6 as a double
% --- Executes during object creation, after setting all properties.
function edit6_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit6 (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 edit7_Callback(hObject, eventdata, handles)
% hObject handle to edit7 (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 edit7 as text
% str2double(get(hObject,'String')) returns contents of edit7 as a double
% --- Executes during object creation, after setting all properties.
function edit7_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit7 (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 edit8_Callback(hObject, eventdata, handles)
% hObject handle to edit8 (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 edit8 as text
% str2double(get(hObject,'String')) returns contents of edit8 as a double
% --- Executes during object creation, after setting all properties.
function edit8_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit8 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called