You are on page 1of 19

P.

MADHAVILATHA
183002110

EXPNO: 5 SPECTRUM ANALYSIS USING DFT


DATE:

AIM:
To analyse the spectrum of a signal using DFT using MATLAB.

SOFTWARE AND HARDWARE TOOLS REQUIRED:


PC with MATLAB software (with GUI) installed.

PROCEDURE:
1.Open MATLAB and open GUIDE by typing ‘guide’ in the command window.
2.Open new UI in the GUIDE layout editor.
3.Display the names of UI component in the palette.
4.set the window size in GUIDE.
5.Layout the simple GUIDE UI.
6.Add necessary components to the UI as per the requirements.
7.Align the components and label them appropriately.
8.Save the UI layout.
9.Code the behaviour of the GUIDE layout by adding the desired m-code to the
call back functions.
10.Save the UI layout and the m-code with the same file name.
11.Run either .m file or .fig file.
12.Verify the output.
P.MADHAVILATHA
183002110

M-CODE:
To compute DFT of a signal using MATLAB, FFT algorithm is used.
ANALYSIS 1: SPECTRUM OF COMPOSITE SIGNAL
function varargout = ex441(varargin)
% EX441 MATLAB code for ex441.fig
% EX441, by itself, creates a new EX441 or raises the existing
% singleton*.
%
% H = EX441 returns the handle to a new EX441 or the handle to
% the existing singleton*.
%
% EX441('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in EX441.M with the given input arguments.
%
% EX441('Property','Value',...) creates a new EX441 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before ex441_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to ex441_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 ex441

% Last Modified by GUIDE v2.5 03-Feb-2020 13:35:16

% Begin initialization code - DO NOT EDIT


gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @ex441_OpeningFcn, ...
'gui_OutputFcn', @ex441_OutputFcn, ...
'gui_LayoutFcn', [] , ...
P.MADHAVILATHA
183002110

'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 ex441 is made visible.


function ex441_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 ex441 (see VARARGIN)

% Choose default command line output for ex441


handles.output = hObject;

% Update handles structure


guidata(hObject, handles);

% UIWAIT makes ex441 wait for user response (see UIRESUME)


% uiwait(handles.figure1);

% --- Outputs from this function are returned to the command line.
function varargout = ex441_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)
P.MADHAVILATHA
183002110

% Get default command line output from handles structure


varargout{1} = handles.output;

% --- Executes on slider movement.


function slider1_Callback(hObject, eventdata, handles)
% hObject handle to slider1 (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,'Value') returns position of slider


% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
fa=get(handles.slider1,'value');
f1=5000*fa;
n=0:99;
x=sin(2*pi*n*f1/100000);
set(handles.edit1,'string',num2str(round(f1)));
plot(handles.axes1,n,x);
axes(handles.axes1);
xlabel('time');ylabel('amplitude');title('signal 1');axis tight

% --- Executes during object creation, after setting all properties.


function slider1_CreateFcn(hObject, eventdata, handles)
% hObject handle to slider1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.


if isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end

% --- Executes on slider movement.


function slider2_Callback(hObject, eventdata, handles)
% hObject handle to slider2 (see GCBO)
P.MADHAVILATHA
183002110

% eventdata reserved - to be defined in a future version of MATLAB


% handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider


% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
fb=get(handles.slider2,'value');
f2=10000*fb;
n=0:99;
x2=sin(2*pi*n*f2/100000);
set(handles.edit2,'string',num2str(round(f2)));
plot(handles.axes2,n,x2);
axes(handles.axes2);
xlabel('time');ylabel('amplitude');title('signal 2');axis tight

% --- Executes during object creation, after setting all properties.


function slider2_CreateFcn(hObject, eventdata, handles)
% hObject handle to slider2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.


if isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end

% --- 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)
fa=get(handles.slider1,'value');
fb=get(handles.slider2,'value');
f1=round(10000*fa);
f2=round(10000*fb);
P.MADHAVILATHA
183002110

n1=0:99;
y=sin(2*pi*n1*f1/8000)+sin(2*pi*n1*f2/8000);
plot(handles.axes3,n1,y,'k');
axes(handles.axes3);
xlabel('time');ylabel('magnitude');title('composite signal');axis tight

% --- 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)
fa=get(handles.slider1,'value');
fb=get(handles.slider2,'value');
f1=10000*fa;
f2=10000*fb;
n1=0:99;
n2=0:255;
y=sin(2*pi*n1*f1/80000)+sin(2*pi*n1*f2/80000);
z=fft(y,512);
k=n2*8000/512;
plot(handles.axes4,k,abs(z(1:256)),'r');
axes(handles.axes4);
xlabel('frequency');ylabel('magnitude');title('spectrum of the composite
signal');axis tight

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)
P.MADHAVILATHA
183002110

% 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

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
P.MADHAVILATHA
183002110

ANALYSIS 2: SPECTRUM OF A NOISY SIGNAL


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

% Last Modified by GUIDE v2.5 03-Feb-2020 15:30:02

% Begin initialization code - DO NOT EDIT


gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @exp442_OpeningFcn, ...
'gui_OutputFcn', @exp442_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
P.MADHAVILATHA
183002110

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 exp442 is made visible.


function exp442_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 exp442 (see VARARGIN)

% Choose default command line output for exp442


handles.output = hObject;

% Update handles structure


guidata(hObject, handles);

% UIWAIT makes exp442 wait for user response (see UIRESUME)


% uiwait(handles.figure1);

% --- Outputs from this function are returned to the command line.
function varargout = exp442_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;
P.MADHAVILATHA
183002110

% --- Executes on slider movement.


function slider1_Callback(hObject, eventdata, handles)
% hObject handle to slider1 (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,'Value') returns position of slider


% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
fa=get(handles.slider1,'value');
f1=1000*fa;
n=0:99;
x=sin(2*pi*n*f1/10000);
plot(handles.axes1,n,x);
axes(handles.axes1);
xlabel('time');ylabel('amplitude');title('signal 1');axis tight
f1=round(1000*fa);
set(handles.edit1,'string',num2str(f1));

% --- Executes during object creation, after setting all properties.


function slider1_CreateFcn(hObject, eventdata, handles)
% hObject handle to slider1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.


if isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end

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)
P.MADHAVILATHA
183002110

% 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

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'))
P.MADHAVILATHA
183002110

set(hObject,'BackgroundColor','white');
end

% --- 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)fa=char(get(handles.slider1,'value'));
fa=get(handles.slider1,'value');
f1=1000*fa;
n1=0:99;
n2=0:255;
y=sin(2*pi*n1*f1/8000);
n=str2num(char(get(handles.edit2,'string')));
yn=awgn(y,n,'measured');
z=fft(yn,512);
k=n2*8000/512;
plot(handles.axes2,k,abs(z(1:256)),'r');
axes(handles.axes2);
xlabel('frequency');ylabel('magnitude');title('spectrum of the noisy signal');axis
tight

% --- 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)
n=str2num(char(get(handles.edit2,'string')));
fa=get(handles.slider1,'value');
f1=round(1000*fa);
set(handles.edit1,'string',num2str(f1));
n1=0:99;
y=sin(2*pi*n1*f1/8000);
yn=awgn(y,n,'measured');
plot(handles.axes3,n1,yn,'k');
axes(handles.axes3);
xlabel('time');ylabel('amplitude');title('noisy signal');axis tight
P.MADHAVILATHA
183002110

ANALYSIS 3: SPECTRUM OF A VOICE SIGNAL


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

% Last Modified by GUIDE v2.5 04-Feb-2020 14:57:27

% Begin initialization code - DO NOT EDIT


gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @ex53_OpeningFcn, ...
'gui_OutputFcn', @ex53_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
P.MADHAVILATHA
183002110

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 ex53 is made visible.


function ex53_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 ex53 (see VARARGIN)

% Choose default command line output for ex53


handles.output = hObject;

% Update handles structure


guidata(hObject, handles);

% UIWAIT makes ex53 wait for user response (see UIRESUME)


% uiwait(handles.figure1);

% --- Outputs from this function are returned to the command line.
function varargout = ex53_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)
P.MADHAVILATHA
183002110

% eventdata reserved - to be defined in a future version of MATLAB


% handles structure with handles and user data (see GUIDATA)
[filename pathname]=uigetfile({'*.wav'},'file selector');
fullpathname=strcat(pathname,filename);
[voice, fs]=audioread(fullpathname);
t=(0:length(voice)-1)/fs;
plot(handles.axes1,t,voice);
axes(handles.axes1);
xlabel('time(sec)'),ylabel('Amplitude');title('Audio signal');axis tight
set(handles.edit1,'string',filename);
N=length(voice);
vf=fft(voice);
vfms=abs(vf(1:length(vf)/2));
f=(0:N-1)*fs/N;
plot(handles.axes2,f(1:length(vf)/2),vfms);
axes(handles.axes2);
xlabel('frequency(Hz)');ylabel('Magnitude');title('spectrum of Audio');axis tight

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
P.MADHAVILATHA
183002110

OUTPUT:

Spectrum of a Composite signal:


P.MADHAVILATHA
183002110

Spectrum of a noisy signal:


P.MADHAVILATHA
183002110

Spectrum of a Voice signal using DFT:


P.MADHAVILATHA
183002110

INFERENCE:
1.The spectrum of a periodic composite signal is discrete and the frequency
components are clearly visible in the magnitude spectrum plot.
2.The spectrum of a noisy signal is also noisy. However, the frequency
components of the signal are visible in the magnitude spectrum plot. This may
become invisible if voice level increases.
3.The spectrum of a voice is continuous, containing a band of frequencies. The
maximum frequency component in the voice is approximately 4000 Hz. The
strength of the magnitude decrease in the upper end of spectrum.

RESULT:
Thus the spectrum analysis of signals is performed using MATLAB,
displayed using GUIDE and the outputs are verified.

You might also like