forked from burakbayramli/books
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCgfft.m
55 lines (48 loc) · 1.05 KB
/
Cgfft.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
function x=cgfft(x,npt,direction)
% Function computes the DFT of a sequence using radix2 FFT
% with constant geometry
% in-place bit reverse shuffling of data
j=2;
for n=2:npt
if n<j
t = x(j-1);
x(j-1) = x(n-1);
x(n-1) = t;
end
k = npt/2;
while k<j-1
j = j-k;
k = k/2;
end
j = j+k;
end
m = log2(npt); % calculate the number of stages: m=log2(npt)
w = exp(direction*2*pi*(0:npt/2-1)/npt*i); % pre-compute the twiddle factors
% perform the FFT computation for each stage
for d=1:m
n = 1;
dk = 2^(d-1);
dr = npt/(2^d);
for k=0:dk-1
p = (k*npt)/(2^d)+1;
for r=1:dr
if rem(d,2)~=0
t = x(2*n)*conj(w(p));
x1(n) = x(2*n-1)+t;
x1(npt/2+n) = x(2*n-1)-t;
else
t = x1(2*n)*conj(w(p));
x(n) = x1(2*n-1)+t;
x(npt/2+n) = x1(2*n-1)-t;
end
n = n+1;
end
end
end
if rem(m,2)~=0
x = x1;
end
%If inverse fft is desired divide each coefficient by npt
if direction==-1
x = x /npt;
end