I need help to just modify this code to satisfy requirements for (i) and (ii) Co
ID: 2079916 • Letter: I
Question
I need help to just modify this code to satisfy requirements for (i) and (ii)
Code:
function [kp Ak Bk]=DisFT(t,f)
N=length(f)/2
dt=(t(2*N)-t(1))/(2*N-1)
kp=[0:N]
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);
end
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 Ak and Bk as a function of frequency vk-k/t . Execute this program for the following data: 0.250.5 0.751.0 1.25 1.5 1.75 2.0 0.250.50.75 1.01.25 1.51.75 2.0 f(t)Explanation / Answer
function [kp Ak Bk]=DisFT(t,f)
% Uniformity checking
spacing=f(2)-f(1);
x=0;
for i=2:1:length(f)
if f(i)~= f(i-1)+spacing
x=x+1;
end
end
if x~=0
fprintf('Error data points are not uniformly spaced. ');
else
N=length(f);
if mod(N,2)==1
f(N+1)=0;
t(N+1)=t(N)+(t(N)-t(N-1));
end
N=length(f)/2
if mod(N,2)==1
f(N+1)=0;
end
dt=(t(2*N)-t(1))/(2*N-1)
kp=[0:N]
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);
end
end
OUTPUT:
t=0:0.25:2;
f=t;
DisFT(t,f)
N =
5
dt =
0.2500
kp =
0 1 2 3 4 5
Ak =
0
Ak =
0 0 0 0 0 0
ans =
0 1 2 3 4 5
>>
or
f(6)=2
f =
0 0.2500 0.5000 0.7500 1.0000 2.0000 1.5000 1.7500 2.0000
>> DisFT(t,f)
Error data points are not uniformly spaced.
>>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.