-
Notifications
You must be signed in to change notification settings - Fork 6
/
easy-interface.lisp
95 lines (88 loc) · 3.01 KB
/
easy-interface.lisp
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
85
86
87
88
89
90
91
92
93
94
95
(in-package "NAPA-FFT.IMPL")
(declaim (inline copy-or-replace))
(defun copy-or-replace (src dst)
(cond ((eql src dst) dst)
(dst
(replace dst src))
(t (copy-seq src))))
(defun %bit-reverse-double (vec dst size)
(declare (type real-sample-array vec)
(type (or null real-sample-array) dst)
(type index size))
(assert (>= (length vec) size))
(let* ((dst (copy-or-replace vec dst))
(n size)
(fun (%ensure-reverse n 'real-sample)))
(assert (>= (length dst) size))
(funcall fun dst 0)))
(defun %bit-reverse-complex (vec dst size)
(declare (type complex-sample-array vec)
(type (or null complex-sample-array) dst)
(type index size))
(assert (>= (length vec) size))
(let* ((dst (copy-or-replace vec dst))
(n size)
(fun (%ensure-reverse n)))
(assert (>= (length dst) size))
(funcall fun dst 0)))
(defun bit-reverse (vec &optional dst (size (length vec)))
(etypecase vec
(complex-sample-array
(%bit-reverse-complex vec dst size))
(real-sample-array
(%bit-reverse-double vec dst size))))
(defun get-window-type (window)
(etypecase window
(complex-sample-array 'complex-sample)
(real-sample-array 'real-sample)))
(defun fft (vec &key dst
size
(in-order t) (scale nil) (window nil))
(declare (type (or null complex-sample-array) dst))
(let* ((n (or size (length vec)))
(old vec)
(vec (complex-samplify vec n))
(dst (if (or (eql old vec) dst)
(copy-or-replace vec dst)
vec)))
(declare (type size n))
(assert (power-of-two-p n))
(assert (>= (length vec) n))
(assert (>= (length dst) n))
(if window
(funcall (get-windowed-fft n (get-window-type window)
:scale scale
:in-order nil)
dst window)
(funcall (get-fft n :scale scale :in-order nil)
dst))
(if in-order
(bit-reverse dst dst n)
dst)))
(defun ifft (vec &key dst
size
(in-order t) (scale t) (window nil))
(declare (type (or null complex-sample-array) dst))
(let* ((n (or size (length vec)))
(old vec)
(vec (complex-samplify vec n))
(dst (if (or (eql old vec) dst)
(copy-or-replace vec dst)
vec)))
(declare (type size n))
(assert (power-of-two-p n))
(assert (>= (length vec) n))
(assert (>= (length dst) n))
(when in-order
(bit-reverse dst dst n))
(if window
(funcall (get-windowed-fft n (get-window-type window)
:forward nil
:scale scale
:in-order nil)
dst window)
(funcall (get-fft n
:forward nil
:scale scale
:in-order nil)
dst))))