-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFS_fft.m
85 lines (61 loc) · 1.25 KB
/
FS_fft.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
close all
clear all
clc
set(0,'DefaultAxesFontSize',30); % set the font size of plot window
%% single period defination
dT=0.001;
t1=dT:dT:1;
t2=1+dT:dT:2;
% t2=1+dT:dT:6;
x1=5*ones(size(t1));
x2=-5*ones(size(t2));
x=[x1 x2];
t=[t1 t2];
N0=length(t);
%%
T=N0*dT;
w0=2*pi/T;
%% exponential FS
Nk=length(t);
k=-floor(Nk/2):floor(Nk/2)-1;
for ii=1:length(k)
Ck(ii)=(1/T)*trapz(t, x.*exp(-j*k(ii)*w0*t));
end
figure(1)
title('FS analytical')
subplot(211)
stem(k,abs(Ck))
xlabel('k')
xlim([-50 50])
hold on
subplot(212)
stem(k,angle(Ck)*180/pi)
xlabel('k')
xlim([-50 50])
hold on
%% FS using fft()
Ckfft=fftshift(fft(x))*dT/T; % scaling factors fft():dT & FS coefficients:1/T
k_fft=-floor(length(Ckfft)/2):floor(length(Ckfft)/2)-1;
figure(1)
subplot(211)
stem(k_fft,abs(Ckfft),'.--')
xlim([-50 50])
ylabel('|Ck|')
subplot(212)
stem(k,angle(Ckfft)*180/pi)
xlabel('k')
hold on
xlim([-50 50])
ylabel('\angleCk')
%%
x_reconstracted=zeros(1,length(t));
for ii=1:length(k)
x_reconstracted=x_reconstracted+Ck(ii)*exp(j*k(ii)*w0*t);
end
figure (2)
plot(t,fftshift(ifft(ifftshift(T*Ckfft)))/dT); % fft() values = T*FSCofficients
% ifft() scaling factor= 1/T
hold on
plot(t,x_reconstracted,'--')
xlabel('time')
ylabel('x')