Chapter7_Prob14
Chapter7_Prob14
7.14 Write a MATLAB user-defined function that calculates the real Discrete Fourier Transform, accord-
ing to Eqs. (7.45), of a function given by a finite number of points. Name the function Real_DFT(t,f)
where the input arguments t and f are vectors with the values of the independent and dependent variables
of the data points, respectively. The program should have the following features.
(i) Check to make sure that the user has entered an even number, 2N, of values (where N is an integer),
and if not, must modify the input data by adding a zero entry at the end for f so that there are 2N values.
(ii) Check to make sure that the data points are equally spaced. If not, the program should output an error
message.
(iii) Display stem plots of A k and B k as a function of frequency ν k = k ⁄ τ .
Execute this program for the following data:
t 0 0.25 0.5 0.75 1.0 1.25 1.5 1.75 2.0
f(t) 0 0.25 0.5 0.75 1.0 1.25 1.5 1.75 2.0
Solution
The user-defined function Real_DFT(t,f) :
function Real_DFT(t,f)
% Real_DFT determines the discrete Fourier Transform of a function
% given by a finite number of points by using Eqs. (7.45).
% Input variables:
% t A vector with the values of the independent variable.
% f A vector with the values of the dependent variable.
% Output:
% A stem plot of the coefficients Ak and Bk as a function of frequncy.
%N=length(f)/2;
N2=length(t);
N=N2/2;
dt=t(2)-t(1);
Flag=0;
for i=3:N2
if t(i)-t(i-1) ~=dt
Flag=1;
Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis
for testing or instructional purposes only to students enrolled in courses for which the textbook
has been adopted. Any other reproduction or translation of this work beyond that permitted by
Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the
copyright owner is unlawful.
2
break
end
end
if Flag == 1
disp(' ERROR: The data points are not equally spaced')
else
if N-fix(N) ~= 0
t(N2+1)=t(N2)+dt;
f(N2+1)=0;
N=(N2+1)/2;
end
kp=[0:N];
nuk=kp/(2*N*dt);
Ak(1)=0; Ak(N+1)=0;
Bk(1)=sum(f(1:2*N))/(2*N);
for k=2:N
Ak(k)=0; Bk(k)=0;
for j=1:2*N
Ak(k)=Ak(k)+f(j)*sin(pi*(k-1)*t(j)/(dt*N));
Bk(k)=Bk(k)+f(j)*cos(pi*(k-1)*t(j)/(dt*N));
end
Ak(k)=Ak(k)/N;
Bk(k)=Bk(k)/N;
end
Bk(N+1)=0;
for j=1:2*N
Bk(N+1)=Bk(N+1)+f(j)*cos(pi*N*t(j)/(dt*N));
end
Bk(N+1)=Bk(N+1)/(2*N);
subplot(2,1,1)
stem(nuk,Ak,'oc')
Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis
for testing or instructional purposes only to students enrolled in courses for which the textbook
has been adopted. Any other reproduction or translation of this work beyond that permitted by
Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the
copyright owner is unlawful.
3
xlabel('Frequency')
ylabel('A(k)')
subplot(2,1,2)
stem(nuk,Bk,'oc')
xlabel('Frequency')
ylabel('B(k)')
end
clear, clc
t=0:0.25:2;
f=0:0.25:2;
Real_DFT(t,f)
Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis
for testing or instructional purposes only to students enrolled in courses for which the textbook
has been adopted. Any other reproduction or translation of this work beyond that permitted by
Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the
copyright owner is unlawful.
4
0.4
0.2
0
A(k)
-0.2
-0.4
-0.6
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
Frequency
0.5
B(k)
-0.5
-1
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
Frequency
Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis
for testing or instructional purposes only to students enrolled in courses for which the textbook
has been adopted. Any other reproduction or translation of this work beyond that permitted by
Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the
copyright owner is unlawful.