|
| 1 | +Attribute VB_Name = "Module1" |
| 2 | +'Serkan Guer 2023 |
| 3 | +'All rights reserved |
| 4 | +Option Explicit |
| 5 | +Type Complex |
| 6 | + Re As Double |
| 7 | + Im As Double |
| 8 | +End Type |
| 9 | +'Radix 2 recursive |
| 10 | +Sub F(N As Long, s As Long, q As Long, d As Long, x() As Complex) |
| 11 | + |
| 12 | + Dim m As Long, p As Long, theta0 As Double |
| 13 | + Dim wp As Complex, a As Complex, b As Complex |
| 14 | + |
| 15 | + m = N / 2 |
| 16 | + theta0 = 2 * Application.pi / N |
| 17 | + |
| 18 | + If N > 1 Then |
| 19 | + For p = 0 To m - 1 |
| 20 | + wp.Re = Cos(p * theta0) |
| 21 | + wp.Im = -Sin(p * theta0) |
| 22 | + |
| 23 | + a = x(q + p) |
| 24 | + b = x(q + p + m) |
| 25 | + |
| 26 | + x(q + p).Re = a.Re + b.Re |
| 27 | + x(q + p).Im = a.Im + b.Im |
| 28 | + |
| 29 | + x(q + p + m).Re = (a.Re - b.Re) * wp.Re - (a.Im - b.Im) * wp.Im |
| 30 | + x(q + p + m).Im = (a.Re - b.Re) * wp.Im + (a.Im - b.Im) * wp.Re |
| 31 | + Next p |
| 32 | + |
| 33 | + Call F(N / 2, 2 * s, q, d, x) |
| 34 | + Call F(N / 2, 2 * s, q + m, d + s, x) |
| 35 | + |
| 36 | + ElseIf q > d Then |
| 37 | + Call Swap(x(q), x(d)) |
| 38 | + End If |
| 39 | + |
| 40 | +End Sub |
| 41 | + |
| 42 | +Sub Swap(a As Complex, b As Complex) |
| 43 | + Dim temp As Complex |
| 44 | + temp = a |
| 45 | + a = b |
| 46 | + b = temp |
| 47 | +End Sub |
| 48 | + |
| 49 | +Sub fft(N As Long, x() As Complex) |
| 50 | + Call F(N, 1, 0, 0, x) |
| 51 | +End Sub |
| 52 | + |
| 53 | +Sub ifft(N As Long, x() As Complex) |
| 54 | + Dim k As Long |
| 55 | + For k = 0 To N - 1 |
| 56 | + x(k).Im = -x(k).Im |
| 57 | + Next k |
| 58 | + |
| 59 | + Call F(N, 1, 0, 0, x) |
| 60 | + |
| 61 | + For k = 0 To N - 1 |
| 62 | + x(k).Re = x(k).Re / N |
| 63 | + x(k).Im = -x(k).Im / N |
| 64 | + Next k |
| 65 | +End Sub |
| 66 | + |
| 67 | +Sub GenerateFR() 'fast fourier transform |
| 68 | + Columns("B:H").Select |
| 69 | + Selection.ClearContents |
| 70 | + Range("A1").Select |
| 71 | + |
| 72 | + Application.Calculation = xlCalculationManual |
| 73 | + Application.ScreenUpdating = False |
| 74 | + |
| 75 | + Dim N As Long, i As Long, rw As Long, SampleRate As Long, rng As String |
| 76 | + |
| 77 | + SampleRate = 48000 'Change to correct sample rate |
| 78 | + |
| 79 | + rw = Range("A1").End(xlDown).Row |
| 80 | + Dim x(1048575) As Complex |
| 81 | + |
| 82 | + N = rw |
| 83 | + |
| 84 | + For i = 0 To N - 1 |
| 85 | + x(i).Re = Cells(i + 1, 1).Value |
| 86 | + x(i).Im = 0 |
| 87 | + Next i |
| 88 | + |
| 89 | + Call fft(N, x()) |
| 90 | + |
| 91 | + For i = 0 To N - 1 |
| 92 | + 'Cells(i + 1, 2).Value = WorksheetFunction.Complex(x(i).Re, x(i).Im) |
| 93 | + Cells(i + 1, 2).Value = x(i).Re |
| 94 | + Cells(i + 1, 3).Value = x(i).Im |
| 95 | + Next i |
| 96 | + |
| 97 | + 'Extract magnitude and phase |
| 98 | + Cells(1, 7) = rw |
| 99 | + Cells(1, 8) = SampleRate |
| 100 | + Cells(2, 4).Formula = "=sqrt(B2^2+c2^2)" |
| 101 | + Cells(2, 5).Formula = "=atan2(b2,c2)" |
| 102 | + Cells(2, 6).Formula = "=F1+$H$1/$G$1" |
| 103 | + Cells(2, 7).Formula = "=20*LOG10(D2)+100" 'Change 100 to the preferred SPL dB Offset value |
| 104 | + Cells(2, 8).Formula = "=-180*E2/PI()" |
| 105 | + |
| 106 | + rng = "D2:H" & rw / 2 |
| 107 | + Range("D2:H2").Select |
| 108 | + Selection.AutoFill Destination:=Range(rng) |
| 109 | + Range("G2").Select |
| 110 | + |
| 111 | + Application.ScreenUpdating = True |
| 112 | + Application.Calculation = xlCalculationAutomatic |
| 113 | +End Sub |
| 114 | + |
| 115 | +Sub GenerateImpulse() 'inverse fast fourier transform |
| 116 | + Columns("D:H").Select |
| 117 | + Selection.ClearContents |
| 118 | + Columns("A:A").Select |
| 119 | + Selection.ClearContents |
| 120 | + Range("B1").Select |
| 121 | + |
| 122 | + Application.Calculation = xlCalculationManual |
| 123 | + Application.ScreenUpdating = False |
| 124 | + |
| 125 | + Dim N As Long, i As Long, rw As Long |
| 126 | + |
| 127 | + rw = Range("B1").End(xlDown).Row |
| 128 | + Dim x(1048575) As Complex |
| 129 | + |
| 130 | + N = rw |
| 131 | + |
| 132 | + For i = 0 To N - 1 |
| 133 | + x(i).Re = Cells(i + 1, 2).Value |
| 134 | + x(i).Im = Cells(i + 1, 3).Value |
| 135 | + Next i |
| 136 | + 'Inverse fft |
| 137 | + Call ifft(N, x) |
| 138 | + |
| 139 | + 'Write ifft result to column D |
| 140 | + For i = 0 To N - 1 |
| 141 | + Cells(i + 1, 4) = x(i).Re |
| 142 | + Next i |
| 143 | + |
| 144 | + Application.ScreenUpdating = True |
| 145 | + Application.Calculation = xlCalculationAutomatic |
| 146 | + |
| 147 | +End Sub |
| 148 | + |
| 149 | +Sub dft() 'basic discrete fourier transform |
| 150 | +Dim r As Long, s As Long, k As LongLong, N(1048576) As Double, SumReal As Double, SumImag As Double, pi As Double, w As Double |
| 151 | +Columns("B:H").Select |
| 152 | + Selection.ClearContents |
| 153 | + Range("B1").Select |
| 154 | +Application.ScreenUpdating = False |
| 155 | +Application.Calculation = xlCalculationManual |
| 156 | +pi = Application.pi |
| 157 | + |
| 158 | +For r = 1 To 1048576 |
| 159 | + N(r - 1) = Cells(r, 1).Value |
| 160 | + If Cells(r, 1) = "" Then Exit For |
| 161 | +Next r |
| 162 | +r = r - 1 |
| 163 | + |
| 164 | +For k = 0 To (r / 2) - 1 |
| 165 | + SumReal = 0: SumImag = 0 |
| 166 | + For s = 0 To r - 1 |
| 167 | + w = -2 * pi * k * s / r |
| 168 | + SumReal = SumReal + N(s) * Cos(w) |
| 169 | + SumImag = SumImag + N(s) * Sin(w) |
| 170 | + Next s |
| 171 | + Cells(k + 1, 2) = SumReal: Cells(k + 1, 3) = SumImag |
| 172 | +Next k |
| 173 | + |
| 174 | +Application.ScreenUpdating = True |
| 175 | +Application.Calculation = xlCalculationAutomatic |
| 176 | +End Sub |
0 commit comments