-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsigprocess.c
More file actions
186 lines (157 loc) · 5.27 KB
/
sigprocess.c
File metadata and controls
186 lines (157 loc) · 5.27 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
186
/*
file : sigprocess.c
author : Ids Andriesse (ids.andriesse@gmail.com)
date : 12-02-2020
version : 1.0
*/
/* Fast Fourier Transforms and applications*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "util.h"
#include "sigprocess.h"
#include <complex.h>
#define MAX(a,b) ((a)>(b) ? (a) : (b))
#define MIN(a,b) ((a)<(b) ? (a) : (b))
const double pi = 3.141592653589793238462643383279502884;
/*Fast Fourier Transform using Cooley Tukey algorithm
*The sign of omega (root of unity) gives the direction (see convolveFFT for example) */
complex double *FFT(complex double *a, int n, complex double omega){
if (n == 1){
return a;
}
//Update root of unity
complex double x = cpow(omega, 0);
//Split arrays in even and odd
complex double *a_even = makeComplexArray(n/2);
complex double *a_odd = makeComplexArray(n/2);
for (int i = 0; 2 * i < n; i++){
a_even[i] = a[2*i];
a_odd[i] = a[2*i + 1];
}
//Recursive calls
complex double *y_even = FFT(a_even, n/2, cpow(omega, 2));
complex double *y_odd = FFT(a_odd, n/2, cpow(omega, 2));
complex double *y = makeComplexArray(n);
for (int i = 0; i < n/2; i++){
y[i] = y_even[i] + x*y_odd[i];
y[i + n/2] = y_even[i] - x*y_odd[i];
x = x * omega;
}
return y;
}
/*2D FFT given a matrix*/
complex double **FFT2D(complex double **matrix, int width, int height, complex double omega){
/* width and height must both be powers of two ! */
if (width != powerOfTwo(width) || height != powerOfTwo(height)){
printf("FFT2D: No power of two\n width = %d\n heigth = %d", width, height);
}
//First FFT over rows
complex double *temp = makeComplexArray(width);
for (int row = 0; row < height; row++){
for (int col = 0; col < width; col++){
temp[col] = matrix[row][col];
}
temp = FFT(temp, width, omega);
for (int col = 0; col < width; col++){
matrix[row][col] = temp[col];
}
}
free(temp);
//Then FFT over columns
temp = makeComplexArray(height);
for (int col = 0; col < width; col++){
for (int row = 0; row < height; row++){
temp[row] = matrix[row][col];
}
temp = FFT(temp, width, omega);
for (int row = 0; row < height; row++){
matrix[row][col] = temp[row];
}
}
free(temp);
return matrix;
}
/*Convolution of filter h with signal x */
double *convolveFFT(complex double *h, int len_h, complex double *x, int len_x){
//Lenght of the convolution
int len_y = len_h + len_x - 1;
//Pad arrays to twice the length and the next power of 2
int new_len = len_h > len_x ? powerOfTwo(len_h) : powerOfTwo(len_x);
new_len *= 2;
h = padArray(h, new_len);
x = padArray(x, new_len);
//Give root of primitve root of unity
double angle = 2*pi / new_len;
complex double omega = cos(angle) + sin(angle)*I;
h = FFT(h, new_len, omega);
x = FFT(x, new_len, omega);
//pointWise multiplication
complex double *cy = makeComplexArray(new_len);
for (int i = 0; i < new_len; i++){
cy[i] = h[i] * x[i];
}
//Change sign of angle root of unity for inverse FFT
angle *= -1;
omega = cos(angle) + sin(angle)*I;
//IFFT
cy = FFT(cy, new_len, omega);
double *y = makeDoubleArray(len_y);
for (int i = 0; i < len_y; i++){
y[i] = (cy[i] / new_len);
}
return y;
}
/*Correlation using FFT see convolveFFt for details*/
double *correlateFFT(complex double *h, int len_h, complex double *x, int len_x){
int len_y = len_h + len_x - 1;
int new_len = len_h > len_x ? powerOfTwo(len_h) : powerOfTwo(len_x);
new_len *= 2;
h = padArray(h, new_len);
x = padArray(x, new_len);
//Give root of primitve root of unity
double angle = 2*pi / new_len;
complex double omega = cos(angle) + sin(angle)*I;
h = FFT(h, new_len, omega);
x = FFT(x, new_len, omega);
//pointWise multiplication using conjugate of h (flipped filter)
complex double *cy = makeComplexArray(new_len);
for (int i = 0; i < new_len; i++){
cy[i] = conj(h[i]) * x[i];
}
//Change sign of angle root of unity for inverse FFT
angle *= -1;
omega = cos(angle) + sin(angle)*I;
//IFFT
cy = FFT(cy, new_len, omega);
double *y = makeDoubleArray(len_y);
for (int i = 0; i < len_y; i++){
y[i] = (cy[i] / new_len);
}
return y;
}
/*This function performs a correlation using FFT and then corrects it
* such that the final values are in range [-1, 1] (pearson correlation coefficient) */
double *pearsonCorrelator(double *h, int len_h, double *x, int len_x){
int len_y = len_x - len_h + 1;
double *cor = correlateFFT(h, len_h, x, len_x);
double *y = makeDoubleArray(len_y);
double h_bar = average(h, len_h);
for (int d = 0; d < len_y; d++){
double sum_x = 0;
double sum_h = 0;
double sum_num = 0;
double x_bar = getXBar(x, len_x, len_h, d);
for (int i = 0; i < len_h; i++){
sum_x += pow(x[i + d] - x_bar, 2);
sum_h += pow(h[i] - h_bar, 2);
sum_num += ((x[i + d]*h_bar) + (x_bar*h[i]) - (x_bar*h_bar));
}
sum_x = sqrt(sum_x);
sum_h = sqrt(sum_h);
double denom = sum_h * sum_x;
double num = cor[d] - sum_num;
y[d] = num / denom;
}
return y;
}