-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdft.h
682 lines (523 loc) · 15.9 KB
/
dft.h
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
#ifndef DFT_H
#define DFT_H
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#define PI 3.14159
//this function could be used to forbid memory corruption but I am not quite sure if works.........
double* memallec(double* array, int* size, int index,int size_index)
{
int i = 0;
FILE* file;
file = fopen("errorlog.txt", "a+");
if(index > size[size_index] - 1)
{
fprintf(file, "Memory Corruption please check your boundaries, the size %d is too small \n in the index %d \n Tried to access %d ", size[size_index], size_index, index);
printf("Something gone wrong \n");
printf("I did that for you but you should check your memory allocation \n");
printf("Wanted to access %d but size is only %d", size[size_index], index);
printf("Changed the size to %d", 2*size[size_index] + index);
double* swap = (double*) malloc(sizeof(double) * (size[size_index] +1));
for(i=0; i < size[size_index]; i++)
{
swap[i] = array[i];
}
double* newsth= (double*) malloc(sizeof(double) *( 2*size[size_index] +index + 1));
for(i=0; i < size[size_index]; i++)
{
newsth[i] = array[i];
}
free(array);
size[0] = 2*size[size_index] + index;
fclose(file);
return newsth;
}
else
{
fclose(file);
return array;
}
}
//comlex multiplication of two vectors with the same length -> vector[i << 1] is Re and vector[(i << 1) + 1] is the Im
int complex_mult_mat(int size, double* input_1 , double* input_2, double* output)
{
int i = 0;
for(; i < size; i++)
{
output[i << 1] = input_1[i << 1] * input_2[i<<1] - input_1[(i << 1) +1]*input_2[(i << 1) +1];
output[(i << 1) +1] = input_1[i << 1] * input_2[(i << 1) +1] + input_1[( i << 1) +1]*input_2[(i<<1)];
if(((i<<1)+1) >= size*2 +1)
{
printf("!! SEGMENTATION FAULT IN complex_mult_mat !! \n");
printf("wanted to access: %d \t But size is %d \n", (i << 1) +1, size);
return -1;
}
}
return 1;
}
//complex multiplikation , the input is still a vector with vector[i << 1] = Re and vector[(i << 1) +1] is the Im but The output Re is the Real part and Im is the imaginary Output
int complex_mult_vec(int size, double* input_1, double* input_2, double* Re, double* Im)
{
int i = 0;
for(; i < size; i++)
{
Re[i] = input_1[i << 1] * input_2[i<<1] - input_1[(i << 1) +1]*input_2[(i << 1) +1];
Im[i] = input_1[i << 1] * input_2[(i << 1) +1] + input_1[( i << 1) +1]*input_2[(i<<1)];
}
return 1;
}
//Used for adding zeros at the end of the vector
//BE CAREFUL THIS FUNCTION GOES OVER THE SIZE OF YOUR "NORMAL" ARRAY ! WHEN YOU ALLOCATE YOUR ARRAY MAKE SURE THAT YOU HAVE DOUBLE THE SIZE YOU THINK IT SHOULD HAVE
void zeroadding(int size, double* input, double* output)
{
int i = 0;
int size_array[1] = size;
for(; i < size; i++)
{
output[i] = input[i];
output[i + size] = 0;
}
}
//just works for real signals !!!! (input is just a one dimensional vector
int dft(int size,double* input,double* output)
{
int i = 0;
int x = 0;
int computed = 0;
int b = 0;
int j = 0;
double* coef_cos = (double*) malloc(sizeof(double) * (size*size + size + 1));
double* coef_sin = (double*) malloc(sizeof(double) * (size*size + size + 1));
double turn = 0;
double turn_perc = 0;
//finding the coeffizients
for(;i < size; i++)
{
//Kernel Vorbereitung -> später werden diese Werte direkt in den Kernel geschrieben!
for(x=0;x < size; x++)
{
computed = 0;
turn = (((double)i*(double)x/(double)size));
turn_perc = turn - (int) turn;
if(turn_perc == 0 || turn_perc == 1)
{
coef_cos[i*size +x] = 1;
coef_sin[i*size +x] = 0;
computed++;
}
if(turn_perc == 0.25)
{
coef_cos[i*size +x] = 0;
coef_sin[i*size +x] = -1;
computed++;
}
if(turn_perc == 0.5)
{
coef_cos[i*size +x] = -1;
coef_sin[i*size +x] = 0;
computed ++;
}
if(turn_perc == 0.75)
{
coef_cos[i*size +x] = 0;
coef_sin[i*size +x] = 1;
computed++;
}
if(computed == 0)
{
coef_cos[i*size +x] = cos(turn*2*M_PI);
coef_sin[i*size +x] = sin(turn*2*M_PI);
computed++;
}
if(computed > 1)
{
printf("Something went horrible wrong in the dft \n");
return -1;
}
}
j++;
}
//The real transformation
for(i=0; i < size; i++)
{
for(x=0; x < size; x++)
{
output[i << 1] += input[x]*coef_cos[i*size +x];
output[(i << 1) +1] += input[x]*(coef_sin[i*size +x]*(-1));
}
}
return 1;
free(coef_cos);
free(coef_sin);
}
int idft(int size,double* input,double* output)
{
int i = 0;
int x = 0;
int computed = 0;
int b = 0;
int j = 0;
double* coef_cos = (double*) malloc(sizeof(double) * (size*size + size + 1));
double* coef_sin = (double*) malloc(sizeof(double) * (size*size + size + 1));
double turn = 0;
double turn_perc = 0;
//finding the coeffizients
for(;i < size; i++)
{
//Kernel Vorbereitung -> später werden diese Werte direkt in den Kernel geschrieben!
for(x=0;x < size; x++)
{
computed = 0;
turn = (((double)i*(double)x/(double)size));
turn_perc = turn - (int) turn;
if(turn_perc == 0 || turn_perc == 1)
{
coef_cos[i*size +x] = 1;
coef_sin[i*size +x] = 0;
computed++;
}
if(turn_perc == 0.25)
{
coef_cos[i*size +x] = 0;
coef_sin[i*size +x] = -1;
computed++;
}
if(turn_perc == 0.5)
{
coef_cos[i*size +x] = -1;
coef_sin[i*size +x] = 0;
computed ++;
}
if(turn_perc == 0.75)
{
coef_cos[i*size +x] = 0;
coef_sin[i*size +x] = 1;
computed++;
}
if(computed == 0)
{
coef_cos[i*size +x] = cos(turn*2*M_PI);
coef_sin[i*size +x] = sin(turn*2*M_PI);
computed++;
}
if(computed > 1)
{
printf("Something went horrible wrong in the idft \n");
return -1;
}
}
}
//The real transformation
for(i=0; i < size; i++)
{
for(x=0; x < size; x++)
{
output[i << 1] += (input[x << 1]*coef_cos[i*size +x] - input[(x << 1) +1]*coef_sin[i*size +x])/(double)size;
// output[(i << 1) +1] += (input[x << 1]*(coef_sin[i*size +x]) + input[(x << 1) + 1] * coef_cos[i*size +x])/(double)size;
}
}
free(coef_cos);
free(coef_sin);
return 1;
}
//just works when both vectors have the same length!
int convolute(int size, double* input,double* input_2, double* output)
{
printf("inside \n");
int i = 0;
int x = 0;
int computed = 0;
int b = 0;
int j = 0;
//fill up vectors
double* input_filled = (double*) malloc(sizeof(double)*((2*size) +2));
double* input_2_filled = (double*) malloc(sizeof(double)*((2*size) +2));
printf("array \n");
double turn = 0;
double turn_perc = 0;
zeroadding(size, input, input_filled); //does not leak;
printf("zerroadding \n");
zeroadding(size, input_2, input_2_filled); //does not leak;
printf("zeroadding 2\n");
size = 2*size +1;
printf("size refreshed \n");
int coef_size = (size)*(size) + size + 1;
int trans_size = 2*(size) + 1;
int conv_size = 2*(size) + 1;
printf("after some sit \n");
int size_array[3];
size_array[0] = coef_size;
size_array[1] = trans_size;
size_array[2] = conv_size;
double* coef_cos = (double*) malloc(sizeof(double) * (coef_size));
double* coef_sin = (double*) malloc(sizeof(double) * (coef_size));
double* trans_one = (double*) malloc(sizeof(double) *(trans_size));
double* trans_two = (double*) malloc(sizeof(double)* (trans_size));
//convolution result
double* conv = (double*) malloc(sizeof(double)*conv_size);
//finding the coeffizients
for(i=0;i < size; i++)
{
//Kernel Vorbereitung -> später werden diese Werte direkt in den Kernel geschrieben!
for(x=0;x < size; x++)
{
printf("still on the run %d \n", i);
computed = 0;
turn = (((double)i*(double)x/(double)size));
turn_perc = turn - (int) turn;
// printf("size = %d \t allokiert = %d \tZugriff auf = %d\n",size, size*size + size +2, i*size + x);
if(turn_perc == 0 || turn_perc == 1)
{
coef_cos[i*size +x] = 1;
coef_sin[i*size +x] = 0;
computed++;
}
if(turn_perc == 0.25)
{
coef_cos[i*size +x] = 0;
coef_sin[i*size +x] = -1;
computed++;
}
if(turn_perc == 0.5)
{
coef_cos[i*size +x] = -1;
coef_sin[i*size +x] = 0;
computed ++;
}
if(turn_perc == 0.75)
{
coef_cos[i*size +x] = 0;
coef_sin[i*size +x] = 1;
computed++;
}
if(computed == 0)
{
coef_cos[i*size +x] = cos(turn*2*M_PI);
coef_sin[i*size +x] = sin(turn*2*M_PI);
computed++;
}
if(computed > 1 || (i*size +x) >= coef_size)
{
printf("Something went horrible wrong in the idft \n");
printf("Wanted to acces %d but size is only %d", i*size +x, size*size + size +2);
return -1;
}
// coef_cos = memallec(coef_cos, size_array, i*size + x, 0);
// coef_sin = memallec(coef_sin, size_array, i*size +x, 0);
}
}
//Transformation
for(i=0; i < size; i++)
{
printf("on the run again %d\n", i);
for(x=0; x < size; x++)
{
trans_one[i << 1] += input_filled[x]*coef_cos[i*size +x];
trans_one[(i << 1) +1] += input_filled[x]*(coef_sin[i*size +x]*(-1));
trans_two[i << 1] += input_2_filled[x]*coef_cos[i*size +x];
trans_two[(i << 1) +1] += input_2_filled[x]*(coef_sin[i*size +x]*(-1));
if((i << 1) +1 >= trans_size-1 || i*size +x >= coef_size-1)
{
printf("SEGMENATION FAULT IN CONVOLOTION \n");
printf("wanted to access %d but size is only %d", (i << 1) +1, trans_size);
printf("wanted to accces %d but size is only %d", i*size +x, coef_size);
}
}
}
//The multiplikation itself -> Convoltuon
complex_mult_mat(size, trans_one, trans_two, conv);
idft(size, conv, output);
free(coef_cos);
free(coef_sin);
free(trans_one);
free(trans_two);
free(conv);
free(input_filled);
free(input_2_filled);
}
int convolute_td(int size, double* input , double* input_2, double* convolute)
{
int i = 0;
int x = 0;
double* input_fill = (double*) malloc(sizeof(double) * (3*size +1));
double* input_fill_2 = (double*) malloc(sizeof(double) * (3*size +1));
zeroadding(size, input, input_fill);
zeroadding(size, input_2, input_fill_2);
size = 2*size +1;
for(; i < size; i++)
{
for(x=0; x < i; x++)
{
convolute[i] += input_fill[i-x] * input_fill_2[size - x];
}
}
free(input_fill);
free(input_fill_2);
return 1;
}
int findcofs(int size, double* coef_cos, double* coef_sin)
{
int x = 0;
int i = 0;
int computed = 0;
int b = 0;
double turn = 0;
double turn_perc = 0;
for(i=0;i < size; i++)
{
//Kernel Vorbereitung -> später werden diese Werte direkt in den Kernel geschrieben!
for(x=0;x < size; x++)
{
computed = 0;
turn = (((double)i*(double)x/(double)size));
turn_perc = turn - (int) turn;
if(turn_perc == 0 || turn_perc == 1)
{
coef_cos[i*size +x] = 1;
coef_sin[i*size +x] = 0;
computed++;
}
if(turn_perc == 0.25)
{
coef_cos[i*size +x] = 0;
coef_sin[i*size +x] = -1;
computed++;
}
if(turn_perc == 0.5)
{
coef_cos[i*size +x] = -1;
coef_sin[i*size +x] = 0;
computed ++;
}
if(turn_perc == 0.75)
{
coef_cos[i*size +x] = 0;
coef_sin[i*size +x] = 1;
computed++;
}
if(computed == 0)
{
coef_cos[i*size +x] = cos(turn*2*M_PI);
coef_sin[i*size +x] = sin(turn*2*M_PI);
computed++;
}
if(computed > 1)
{
printf("Something went horrible wrong in the idft \n");
return -1;
}
}
}
return 1;
}
int conv_cofs(int size,double* input, double* input_2, double* coef_cos, double* coef_sin, double* output)
{
double* trans_one = (double*) malloc(sizeof(double) * ((size << 1) +1));
double* trans_two = (double*) malloc(sizeof(double) * ((size << 1) +1));
double* input_filled = (double*) malloc(sizeof(double) * (3*size +1));
double* input_2_filled = (double*) malloc(sizeof(double) * (3*size +1));
zeroadding(size, input, input_filled);
zeroadding(size, input_2, input_2_filled);
size = 2*size +1;
int i = 0;
int x = 0;
double* conv = (double*) malloc(sizeof(double) * (size << 1) +2);
//Transformation
for(i=0; i < size; i++)
{
for(x=0; x < size; x++)
{
trans_one[i << 1] += input_filled[x]*coef_cos[i*size +x];
trans_one[(i << 1) +1] += input_filled[x]*(coef_sin[i*size +x]*(-1));
trans_two[i << 1] += input_2_filled[x]*coef_cos[i*size +x];
trans_two[(i << 1) +1] += input_2_filled[x]*(coef_sin[i*size +x]*(-1));
}
}
//The multiplikation itself -> Convoltuon
for(i=0; i < size; i++)
{
complex_mult_mat(size, trans_one, trans_two, conv);
}
for(i=0; i < size; i++)
{
for(x=0; x < size; x++)
{
output[i << 1] += (conv[x << 1]*coef_cos[i*size +x] - conv[(x << 1) +1]*coef_sin[i*size +x])/(double)size;
// output[(i << 1) +1] += (conv[x << 1]*(coef_sin[i*size +x]) + conv[(x << 1) + 1] * coef_cos[i*size +x])/(double)size;
}
}
free(trans_one);
free(trans_two);
free(input_filled);
free(input_2_filled);
}
void fastdft(int size, double input, double* coef_cos, double* coef_sin, double* Re, double* Im)
{
int i = 0;
int j = 0;
int x = 0;
int g = 0;
double* input_filled = (double*) malloc(sizeof(double) * (2*size +2));
zeroadding(size, input, input_filled);
size = 2*size +1;
double* Re_matrix = (double*) malloc(sizeof(double)*(size*size + size + 2));
double* Im_matrox = (double*) malloc(sizeof(double)*(size*size + size + 2));
for(j=0; j < size; j++)
{
for(i=j; i < size; i++)
{
Re_matrix[size*j + i] = coef_cos[i*size + j] * input_filled[i];
Im_matrix[size*j + i] = coef_sin[i*size + j] * input_filled[i];
Re_matrix[size*i +j] = Re_matrix[size*j +i];
Im_matrix[size*i +j] = Im_matrix[size*j +i];
}
}
for(j = 0; j < size; j++)
{
for(i=0; i < size; i++)
{
Re[j] += Re_matrix[size*i +j];
Im[j] += Im_matrix[size*i +j];
}
}
}
int fconv(int size, double* input, double* input_2, double* output, int debug)
{
int z,i = 0;
//Results bigger than size
for(;z < size; z++)
{
for(i=z; i < size; i++)
{
output[z + size] = input[i] * input_2[size - i + z];
if(debug == 1)
{
printf("output[%i] = input[%i] * input_2[%i] \n %f = %f * %f \n \n", z+size,i,size-i+z,output[z+size],input[i],input_2[size-i+z]);
if(z+size > 2*size)
{
printf("memory corruption, wanted to access %d but size is %d", z+size, 2*size);
return -1;
}
if((size - i + z) > size)
{
printf("memory corruption, wanted to access %d but size is %d",(size-i +z), size);
return -1;
}
}
}
}
//Results smaller than size
for(z=0; z < size; z++)
{
for(i=0; i < z; i++)
{
output[z] = input[i] * input_2[z -i];
if(debug == 1)
{
printf("output[%d] = input[%d] * input_2[%d] \n %f = %f * %f",z,i,z-i, output[z],input[i],input_2[z-i]);
}
}
}
return 0;
}
#endif