Homeworks on FFT 90822- Instr. and Meas. for Communication Systems- Gianfranco Miele Name Surname October 15, 2014 1
Name Surname 90822 (Gianfranco Miele): Homeworks on FFT Contents Exercise 1 (Solution)............................................ 3 (a)...................................................... 3 (b)...................................................... 3 (c)...................................................... 4 (d)...................................................... 6 (e)...................................................... 8 (f)...................................................... 9 (g)...................................................... 10 Exercise 2 (Solution)............................................ 13 List of Figures 1 s 1 (t) and s 2 (t) versus time...................................... 3 2 S 1 (f)................................................. 4 3 S 2 (f)................................................. 5 4 S 1 (f) evaluated considering two different transform lengths................... 6 5 S 2 (f) evaluated considering two different transform lengths................... 6 6 S 1 (f) evaluated considering two different windows........................ 7 7 S 2 (f) evaluated considering two different windows........................ 8 8 S tot (f) evaluated considering two different transform lengths. A rectangular window has been applied................................................. 9 9 S tot (f) evaluated considering two different transform lengths. A Hamming window has been applied................................................. 10 10 S 1 (f)................................................. 11 11 S 2 (f)................................................. 11 12 S 1 (f) evaluated considering two different windows (rectangular and Hanning windows).... 13 13 S 2 (f) evaluated considering two different windows (rectangular and Hanning windows).... 13 14 S 1 (f) evaluated considering two different windows (rectangular and flat-top windows)..... 14 15 S 2 (f) evaluated considering two different windows (rectangular and flat-top windows)..... 14 16 GUI implementing the frequency shift of a sinusoidal signal.................... 19 List of Tables Page 2 of 19
Name Surname 90822 (Gianfranco Miele): Homeworks on FFT Exercise 1 Exercise 1 (Solution) (a) The listing below shows the correct MATLAB script. A1=75; %amplitude of s_1(t) f1=609.36; %frequency of s_1(t) A2=1; %amplitude of s_2(t) f2=937.5; %frequency of s_2(t) 5 fs=6000; %sampling rate Ts=1/fs; %sampling period t=[0:ts:63*ts]; %time vector x1=a1*sin(2*pi*f1*t); %s_1 x2=a2*sin(2*pi*f2*t); %s_2 10 plot(t,x1, b, LineWidth,6) %plot of s_1(t) vs time hold on plot(t,x2, r, LineWidth,6) %plot of s_2(t) vs time 15 xlabel( Time [s], FontSize,22) %x-axis label ylabel( Amplitude [V], FontSize,22) %y-axis label legend( s_1(t), s_2(t) ) %legend Figure 1: s 1 (t) and s 2 (t) versus time (b) The listing below shows the correct MATLAB script. Exercise 1 continued on next page... Page 3 of 19
Name Surname 90822 (Gianfranco Miele): Homeworks on FFT Exercise 1 (continued) X1= f f t (x1)/length(x1); %FFT of s_1(t) X2= f f t (x2)/length(x2); %FFT of s_2(t) df=fs/length(x1); %frequency resolution 5 f=[0:length(x1)/2]*df; %vector of frequency bins plot(f,abs(x1(1:length(f))), b, LineWidth,6) %plot of S_1(f) ylabel( Magnitude [V], FontSize,22) 10 plot(f,abs(x2(1:length(f))), r, LineWidth,6) %plot of S_2(f) 15 ylabel( Magnitude [V], FontSize,22) Figure 2: S 1 (f) Regarding Figure 2, analyzing the above reported plot it is possible to highlight that S 1 (f) has the highest peak at 562.5 Hz and its value is 24.71 V and these values don t match with the expected results. In fact from the calculations S 1 (f) have a line spectrum composed by a single tone at 609.36 Hz and its value is 37.5 V. As far as Figure 3 goes, the highest peak is at 937.5 Hz and its value is 0.5 V and these values agree with that obtained from the calculations. In the first case, the analysis is affected by spectral leakage and the amplitude error is referred to as scalloping loss. (c) The listing below shows the correct MATLAB script. Exercise 1 continued on next page... Page 4 of 19
Name Surname 90822 (Gianfranco Miele): Homeworks on FFT Exercise 1 (continued) Figure 3: S 2 (f) X3= f f t (x1,1024)/length(x1); %new FFT of s_1(t) considering %a transform length of 1024 %samples 5 X4= f f t (x2,1024)/length(x1); %new FFT of s_2(t) considering %a transform length of 1024 %samples df=fs/1024; %new frequency resolution f3=[0:1024/2]*df; %new vector of frequency bins 10 plot(f,abs(x1(1:length(f))), b, LineWidth,6) %plot of S_1(f) hold on plot(f3,abs(x3(1:length(f3))), g, LineWidth,6) %new plot of S_1(f) 15 ylabel( Magnitude [V], FontSize,22) legend( 64-points, 1024-points ) %legend plot(f3,abs(x4(1:length(f3))), k, LineWidth,6) %plot of S_2(f) 20 hold on plot(f,abs(x2(1:length(f))), r, LineWidth,6) %new plot of S_2(f) ylabel( Magnitude [V], FontSize,22) 25 legend( 1024-points, 64-points ) %legend Using a higher transform length, we have improved the frequency resolution of our analysis, allowing to obtain a better magnitude estimation. Exercise 1 continued on next page... Page 5 of 19
Name Surname 90822 (Gianfranco Miele): Homeworks on FFT Exercise 1 (continued) Figure 4: S 1 (f) evaluated considering two different transform lengths Figure 5: S 2 (f) evaluated considering two different transform lengths (d) The listing below shows the correct MATLAB script. w=hamming(length(x1)) ; %window definition y1=x1.*w; %windowing s_1(t) y2=x2.*w; %windowing s_2(t) 5 Y1= f f t (y1)/sum(w); %FFT of the windowed signal Exercise 1 continued on next page... Page 6 of 19
Name Surname 90822 (Gianfranco Miele): Homeworks on FFT Exercise 1 (continued) %s_1(t) Y2= f f t (y2)/sum(w); %FFT of the windowed signal %s_2(t) plot(f,abs(x1(1:length(f))), b, LineWidth,6) %plot of S_1(f) 10 hold on plot(f,abs(y1(1:length(f))), r, LineWidth,6) %plot of S_1_windowed(f) ylabel( Magnitude [V], FontSize,22) 15 legend( Rectangular, Hamming ) %legend plot(f,abs(x2(1:length(f))), b, LineWidth,6) %plot of S_2(f) hold on 20 plot(f,abs(y2(1:length(f))), r, LineWidth,6) %plot of S_2_windowed(f) ylabel( Magnitude [V], FontSize,22) legend( Rectangular, Hamming ) %legend 25 Figure 6: S 1 (f) evaluated considering two different windows Regarding Figure 6, analyzing the above reported plot it is possible to highlight that S 1 (f) the Hamming window allows to improve the magnitude estimation. In fact in this case we can found a highest peak at 562.5 Hz with an amplitude value equal to 30.9 V. As a consequence, the measure is affected by scallop loss but its value is lower than that experienced considering a rectangular window. The Hamming window permits to reduce the spectral leakage. As far as Figure 7 goes, the highest peak is at 937.5 Hz and its value is 0.5 V and these values agree with that obtained from the calculations. Exercise 1 continued on next page... Page 7 of 19
Name Surname 90822 (Gianfranco Miele): Homeworks on FFT Exercise 1 (continued) Figure 7: S 2 (f) evaluated considering two different windows (e) The listing below shows the correct MATLAB script. s1=x1+x2; %new signal s_tot(t) S1= f f t (s1)/length(s1); %FFT of s_tot(t) plot(f,20*log10(abs(s1(1:length(f)))), b, LineWidth,6) %plot of S_tot(f) 5 hold on S2= f f t (s1,1024)/length(s1); %new FFT of s_tot(t) %considering a %transform length of 10 %1024 samples plot(f3,20*log10(abs(s2(1:length(f3)))), r, LineWidth,6) %new plot of S_tot(f) ylabel( Magnitude [dbv], FontSize,22) legend( 64-points, 1024-points ) 15 S3= f f t (s1.*w)/sum(w); %FFT of the windowed %version of s_tot(t) plot(f,20*log10(abs(s3(1:length(f)))), b, LineWidth,6) %plot of the FFT of the 20 %windowed version of %s_tot(t) hold on S4= f f t (s1.*w,1024)/sum(w); %new FFT of the 25 %windowed version %of s_tot(t) %considering a Exercise 1 continued on next page... Page 8 of 19
Name Surname 90822 (Gianfranco Miele): Homeworks on FFT Exercise 1 (continued) %transform length of %1024 samples 30 plot(f3,20*log10(abs(s4(1:length(f3)))), r, LineWidth,6) %plot of the new FFT %of the windowed %version of s_tot(t) ylabel( Magnitude [dbv], FontSize,22) 35 legend( 64-points, 1024-points ) Figure 8: S tot (f) evaluated considering two different transform lengths. A rectangular window has been applied. In Figure 9 the weaker signal is more visible. As a consequence, the Hamming window allows to distinctly measure the weaker signal. (f) The listing below shows the correct MATLAB script. fs1=3000; %new sampling rate Ts1=1/fs1; %new sampling period t1=[0:ts1:63*ts1]; %new vector of time samples x1b=a1*sin(2*pi*f1*t1); %resampled signal s_1(t) 5 x2b=a2*sin(2*pi*f2*t1); %resampled signal s_1(t) X1b= f f t (x1b)/length(x1b); %FFT of s1_(t) X2b= f f t (x2b)/length(x2b); %FFT of s1_(t) df1=fs1/length(x1b); %new frequency resolution 10 f4=[0:length(x1b)/2]*df1; %new vector of frequency bins plot(f4,abs(x1b(1:length(f4))), b, LineWidth,6) %plot of S_1(f) Exercise 1 continued on next page... Page 9 of 19
Name Surname 90822 (Gianfranco Miele): Homeworks on FFT Exercise 1 (continued) Figure 9: S tot (f) evaluated considering two different transform lengths. applied. A Hamming window has been ylabel( Magnitude [V], FontSize,22) 15 plot(f4,abs(x2b(1:length(f4))), r, LineWidth,6) 20 ylabel( Magnitude [V], FontSize,22) %plot of S_2(f) Using a sampling rate equal to 3000 Hz it is possible to avoid spectral leakage and it is proved by the Figures 10 and 11. (g) The listing below shows the correct MATLAB script. w=hann(length(x1)) ; y1=x1.*w; y2=x2.*w; %Hanning window %windowing s_1(t) %windowing s_2(t) 5 Y1= f f t (y1)/sum(w); %FFT of the windowed signal %s_1(t) Y2= f f t (y2)/sum(w); %FFT of the windowed signal %s_2(t) plot(f,abs(x1(1:length(f))), b, LineWidth,6) %plot of S_1(f) (rectangular 10 %window) hold on Exercise 1 continued on next page... Page 10 of 19
Name Surname 90822 (Gianfranco Miele): Homeworks on FFT Exercise 1 (continued) Figure 10: S 1 (f) Figure 11: S 2 (f) plot(f,abs(y1(1:length(f))), r, LineWidth,6) 15 ylabel( Magnitude [V], FontSize,22) legend( Rectangular, Hanning ) %plot of S_1(f) (Hanning %window) %legend 20 plot(f,abs(x2(1:length(f))), b, LineWidth,6) %plot of S_2(f) (rectangular Exercise 1 continued on next page... Page 11 of 19
Name Surname 90822 (Gianfranco Miele): Homeworks on FFT Exercise 1 (continued) hold on plot(f,abs(y2(1:length(f))), r, LineWidth,6) %window) 25 %window) xlabel( Frequency [Hz] ) ylabel( Magnitude [V] ) legend( Rectangular, Hanning ) %legend 30 w=flattopwin(length(x1)) ; y1=x1.*w; y2=x2.*w; %plot of S_2(f) (Hanning %Flat-Top window %windowing s_1(t) %windowing s_2(t) 35 Y1= f f t (y1)/sum(w); %FFT of the windowed signal %s_1(t) Y2= f f t (y2)/sum(w); %FFT of the windowed signal %s_2(t) plot(f,abs(x1(1:length(f))), b, LineWidth,6) %plot of S_1(f) (rectangular 40 %window) hold on plot(f,abs(y1(1:length(f))), r, LineWidth,6) 45 ylabel( Magnitude [V], FontSize,22) legend( Rectangular, Flat-Top ) %plot of S_1(f) (Flat-top %window) 50 plot(f,abs(x2(1:length(f))), b, LineWidth,6) %plot of S_2(f) (rectangular %window) hold on plot(f,abs(y2(1:length(f))), r, LineWidth,6) %plot of S_1(f) (Flat-top 55 %window) ylabel( Magnitude [V], FontSize,22) legend( Rectangular, Flat-Top ) As the results depicted in s 12 and 13 concern, it is possible to note that the Hanning window allows to improve the magnitude estimate, reducing the scallop loss. These effect is clearly visibile when the flat-top window is applied. In fact, analyzing the plot depicted in 14 we can measure a magnitude value equal to 37.46 V with a very low scallop loss. On the other hand we have to highlight that this window has a large main lobe not allowing an accurate frequency measurement. Page 12 of 19
Name Surname 90822 (Gianfranco Miele): Homeworks on FFT Exercise 2 Figure 12: S 1 (f) evaluated considering two different windows (rectangular and Hanning windows). Figure 13: S 2 (f) evaluated considering two different windows (rectangular and Hanning windows). Exercise 2 (Solution) The listing below shows the correct MATLAB script. function varargout = homework(varargin) % HOMEWORK MATLAB code for homework.fig % HOMEWORK, by itself, creates a new HOMEWORK or raises the existing % singleton*. 5 % Exercise 2 continued on next page... Page 13 of 19
Name Surname 90822 (Gianfranco Miele): Homeworks on FFT Exercise 2 (continued) Figure 14: S 1 (f) evaluated considering two different windows (rectangular and flat-top windows). Figure 15: S 2 (f) evaluated considering two different windows (rectangular and flat-top windows). % H = HOMEWORK returns the handle to a new HOMEWORK or the handle to % the existing singleton*. % % HOMEWORK( CALLBACK,hObject,eventData,handles,...) calls the local 10 % function named CALLBACK in HOMEWORK.M with the given input arguments. % % HOMEWORK( Property, Value,...) creates a new HOMEWORK or raises the % existing singleton*. Starting from the left, property value pairs are Exercise 2 continued on next page... Page 14 of 19
Name Surname 90822 (Gianfranco Miele): Homeworks on FFT Exercise 2 (continued) % applied to the GUI before homework_openingfcn gets called. An 15 % unrecognized property name or invalid value makes property application % stop. All inputs are passed to homework_openingfcn via varargin. % % *See GUI Options on GUIDE s Tools menu. Choose "GUI allows only one % instance to run (singleton)". 20 % % See also: GUIDE, GUIDATA, GUIHANDLES % Edit the above text to modify the response to help homework 25 % Last Modified by GUIDE v2.5 07-Oct-2014 23:45:16 % Begin initialization code - DO NOT EDIT gui_singleton = 1; gui_state = struct( gui_name, mfilename,... 30 gui_singleton, gui_singleton,... gui_openingfcn, @homework_openingfcn,... gui_outputfcn, @homework_outputfcn,... gui_layoutfcn, [],... gui_callback, []); 35 i f nargin && ischar(varargin{1}) gui_state.gui_callback = str2func(varargin{1}); end i f nargout 40 [varargout{1:nargout}] = gui_mainfcn(gui_state, varargin{:}); else gui_mainfcn(gui_state, varargin{:}); end % End initialization code - DO NOT EDIT 45 % --- Executes just before homework is made visible. function homework_openingfcn(hobject, eventdata, handles, varargin) % This function has no output args, see OutputFcn. 50 % hobject handle to % handles structure with handles and user data (see GUIDATA) % varargin command line arguments to homework (see VARARGIN) 55 % Choose default command line output for homework handles.output = hobject; % Update handles structure guidata(hobject, handles); 60 % UIWAIT makes homework wait for user response (see UIRESUME) % uiwait(handles.1); 65 % --- Outputs from this function are returned to the command line. function varargout = homework_outputfcn(hobject, eventdata, handles) Exercise 2 continued on next page... Page 15 of 19
Name Surname 90822 (Gianfranco Miele): Homeworks on FFT Exercise 2 (continued) % varargout cell array for returning output args (see VARARGOUT); % hobject handle to 70 % handles structure with handles and user data (see GUIDATA) % Get default command line output from handles structure varargout{1} = handles.output; 75 function A_Callback(hObject, eventdata, handles) % hobject handle to A (see GCBO) 80 % handles structure with handles and user data (see GUIDATA) % Hints: get(hobject, String ) returns contents of A as text % str2double(get(hobject, String )) returns contents of A as a double 85 % --- Executes during object creation, after setting all properties. function A_CreateFcn(hObject, eventdata, handles) % hobject handle to A (see GCBO) 90 % handles empty - handles not created until after all CreateFcns called % Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER. i f ispc && isequal(get(hobject, BackgroundColor ), get(0, defaultuicontrolbackgroundcolor )) 95 set(hobject, BackgroundColor, white ); end 100 function f0_callback(hobject, eventdata, handles) % hobject handle to f0 (see GCBO) % handles structure with handles and user data (see GUIDATA) 105 % Hints: get(hobject, String ) returns contents of f0 as text % str2double(get(hobject, String )) returns contents of f0 as a double % --- Executes during object creation, after setting all properties. 110 function f0_createfcn(hobject, eventdata, handles) % hobject handle to f0 (see GCBO) % handles empty - handles not created until after all CreateFcns called 115 % Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER. i f ispc && isequal(get(hobject, BackgroundColor ), get(0, defaultuicontrolbackgroundcolor )) set(hobject, BackgroundColor, white ); end Exercise 2 continued on next page... Page 16 of 19
Name Surname 90822 (Gianfranco Miele): Homeworks on FFT Exercise 2 (continued) 120 function fs_callback(hobject, eventdata, handles) % hobject handle to fs (see GCBO) 125 % handles structure with handles and user data (see GUIDATA) 130 % Hints: get(hobject, String ) returns contents of fs as text % str2double(get(hobject, String )) returns contents of fs as a double % --- Executes during object creation, after setting all properties. function fs_createfcn(hobject, eventdata, handles) % hobject handle to fs (see GCBO) 135 % handles empty - handles not created until after all CreateFcns called % Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER. 140 i f ispc && isequal(get(hobject, BackgroundColor ), get(0, defaultuicontrolbackgroundcolor )) set(hobject, BackgroundColor, white ); end 145 150 function deltaf_callback(hobject, eventdata, handles) % hobject handle to deltaf (see GCBO) % handles structure with handles and user data (see GUIDATA) % Hints: get(hobject, String ) returns contents of deltaf as text % str2double(get(hobject, String )) returns contents of deltaf as a double 155 % --- Executes during object creation, after setting all properties. function deltaf_createfcn(hobject, eventdata, handles) % hobject handle to deltaf (see GCBO) % handles empty - handles not created until after all CreateFcns called 160 % Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER. i f ispc && isequal(get(hobject, BackgroundColor ), get(0, defaultuicontrolbackgroundcolor )) set(hobject, BackgroundColor, white ); 165 end % --- Executes on button press in pushbutton1. function pushbutton1_callback(hobject, eventdata, handles) 170 % hobject handle to pushbutton1 (see GCBO) % handles structure with handles and user data (see GUIDATA) Exercise 2 continued on next page... Page 17 of 19
Name Surname 90822 (Gianfranco Miele): Homeworks on FFT Exercise 2 (continued) axes(handles.axes1); cla; 175 A=str2num(get(handles.A, String )); f0=str2num(get(handles.f0, String )); fs=str2num(get(handles.fs, String )); delta_f=str2num(get(handles.deltaf, String )); 180 t=str2num(get(handles.t, String )); Ts=1/fs; t1=[0:ts:t]; x=a*sin(2*pi*f0*t1).*exp(-j*2*pi*(delta_f)*t1); X=abs( f f t (x))/length(x); 185 df=fs/length(x); f=(0:round(length(x)/2))*df; plot(f,x(1:length(f)), LineWidth,6) ylabel( Magnitude [V], fontsize,22) 190 xlabel( Frequency [Hz], fontsize,22) function t_callback(hobject, eventdata, handles) 195 % hobject handle to t (see GCBO) % handles structure with handles and user data (see GUIDATA) % Hints: get(hobject, String ) returns contents of t as text 200 % str2double(get(hobject, String )) returns contents of t as a double % --- Executes during object creation, after setting all properties. function t_createfcn(hobject, eventdata, handles) 205 % hobject handle to t (see GCBO) % handles empty - handles not created until after all CreateFcns called % Hint: edit controls usually have a white background on Windows. 210 % See ISPC and COMPUTER. i f ispc && isequal(get(hobject, BackgroundColor ), get(0, defaultuicontrolbackgroundcolor )) set(hobject, BackgroundColor, white ); end The GUI is depicted in 16. Page 18 of 19
Name Surname 90822 (Gianfranco Miele): Homeworks on FFT Exercise 2 Figure 16: GUI implementing the frequency shift of a sinusoidal signal. Page 19 of 19