-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfht.c
More file actions
185 lines (161 loc) · 5.12 KB
/
fht.c
File metadata and controls
185 lines (161 loc) · 5.12 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//******************************************
//NAME: Robert Taintor
//DATE: 8/3/07
//PURPOSE: Performs a fast polynomial transform on a group of orthogonal
// polynomials which satisfy a 3 term recurrance
// from uniformly sampled points.
#include <string.h>
#include <math.h>
#include <fftw3.h>
#include "lalgebra.h"
#include "fht.h"
#include "parameters.h"
// Defines the data points
double xk(int k) {
return (double) (((double)(k - LITN) / (double)LITN) * BIGC);
}
// Use Zl to calculate Zl + 1
// Z0 = Zl
// Z1 = Zl + 1
// n is the length of Z0
void calculateFirstZ(double *Z0, double *Z1, int n)
{
memset(Z1, 0, sizeof(double) * n);
int i;
// Normalize the Chebyshev result
for(i=0; i<n; ++i)
Z0[i] *= D0;
// Now compute Z1
for(i=1; i<(n-2); ++i) {
Z1[i] = AL(0) * (1.0/ALPHA * Z0[i+1] - BETA/ALPHA * Z0[i] - GAMMA/ALPHA * Z0[i-1]) + BL(0)*Z0[i];
}
}
// Define An(l) as in the paper
// Result is a 8*n sized vector with each 2n representing a circulant block
void createAn(int n, int l, double *result) {
// Top left is all zeros (we'll zero out everything else while we're at it.
memset(result, 0, sizeof(double) * 8*n);
// Top right is I2n
result[2*n] = 1;
// Bottom left is cl*I2n
result[4*n] = CL(l);
// Bottom right is Cn(wl,vl,ul);
result[6*n] = VL(l);
result[6*n+1] = WL(l);
result[8*n-1] = UL(l);
}
// Needed columns of circulant matrices listed vertically as top left, top right, bottom left, bottom right.
void precomputeRnAndStore(int n, int l, double* result) {
int i;
double *temp = (double*) fftw_malloc(sizeof(double) * 8*n);
double *temp2 = (double*) fftw_malloc(sizeof(double) * 8*n);
createAn(n, n/2+l, result);
for(i=l+n/2-1; i>l; --i) {
createAn(n, i, temp);
fourBcirculantSqMatrixMultiply(result, temp, 4*n, temp2);
memcpy(result, temp2, sizeof(double) * 8*n);
}
fftw_free(temp);
fftw_free(temp2);
}
// Perform a Chebyshev transform in the most naive way possible directly from the data points defined by xl()
void naiveChebyshev(double *data, double *results) {
double Lminus1;
double Lminus2;
double curVal;
int x, y;
memset(results, 0, sizeof(double)*BIGN);
// For each data point
for(x=0; x<=2*LITN; ++x) {
Lminus1 = xk(x) / BIGC;
Lminus2 = 2.0*pow(Lminus1, 2) - 1.0;
// Go through the n Chebyshev polynomials
for(y=0; y<BIGN; ++y) {
curVal = (ALPHA * xk(x) + BETA) * Lminus1 + GAMMA * Lminus2;
results[y] += data[x] * curVal;
Lminus2 = Lminus1;
Lminus1 = curVal;
}
}
}
// A recursive function which performs a Hermite transform in O(n(logn)^2) time
// Z0 and Z1 must be precomputed as defined in the paper.
// The value of l should be first set to 1.
// You must precompute all the necessary Rns.
void performTransform(double* Z0, double* Z1, int n, int l, double* result) {
result[l-1] = Z0[n-1];
result[l] = Z1[n-1];
if (n < 3) return;
// temp to store the new data
double* temp = (double*) fftw_malloc(sizeof(double) * 4*n);
// Combine Z0 and Z1 into Z to get ready for the matrix multiply
double *Z;
Z = (double*) fftw_malloc(sizeof(double) * 4*n);
memcpy(Z, Z0, sizeof(double) * 2*n);
memcpy(Z+n*2, Z1, sizeof(double) * 2*n);
preFourBcirculantVcMatrixMultiply(n, l-1, Z, temp);
fftw_free(Z);
int nover2 = n / 2;
performTransform(Z0+nover2, Z1+nover2, nover2, l, result);
performTransform(temp+nover2, temp+5*nover2, nover2, l+nover2, result);
fftw_free(temp);
return;
}
// Performs a Hermite transform in the most naive way possible directly from the data points given in xl()
void naiveTransform(double *data, double *results) {
memset(results, 0, sizeof(double) * BIGN);
double Lminus1;
double Lminus2;
double curVal;
int x, y;
// For each data point
for(x=0; x<=2*LITN; ++x) {
Lminus1 = 0;
curVal = D0;
// Go through the Hermite polynomials
for(y=0; y<BIGN; ++y) {
results[y] += data[x] * curVal;
Lminus2 = Lminus1;
Lminus1 = curVal;
curVal = (AL(y) * xk(x) + BL(y)) * Lminus1 + CL(y) * Lminus2;
}
}
}
void oneDTransform(double *data, double* result) {
int i;
double *Z0 = (double*) fftw_malloc(sizeof(double) * 2*BIGN);
double *Z1 = (double*) fftw_malloc(sizeof(double) * 2*BIGN);
// Do a Chebyshev Transform
naiveChebyshev(data, Z0+BIGN-1);
Z0[2*BIGN - 1] = 0;
// Expand the data
for(i=0; i<BIGN; ++i)
Z0[i] = Z0[2*BIGN-i-2];
// Find the next data point
calculateFirstZ(Z0, Z1, 2*BIGN);
// Do the second part
performTransform(Z0, Z1, BIGN, 1, result);
fftw_free(Z0);
fftw_free(Z1);
}
void twoDTransform(double *data, int n, double* result) {
int i;
// Initialize the plans for the Fourier Transforms
initFastFouriers(n);
initRns(n);
// Transform the rows
for(i=0; i<=2*n; ++i) {
oneDTransform(&data[n*i], &result[n*i]);
}
//transform the columns
double* column = (double*) fftw_malloc(sizeof(double) * n);
double* temp = (double*) fftw_malloc(sizeof(double) * n);
for(i=0; i<n; ++i) {
getColumnFromSq(result, column, i, n);
oneDTransform(column, temp);
setColumnToSq(temp, result, i, n);
}
fftw_free(column);
fftw_free(temp);
destroyFastFouriers(n);
}