00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include <stdio.h>
00039 #include <stdint.h>
00040 #include <math.h>
00041 #include <string.h>
00042
00043 #include "dt_window.h"
00044
00045 typedef struct {
00046 char *name;
00047 double (* function)(int32_t num, int32_t den);
00048 } ts_window_function;
00049
00050 typedef struct x_dt_window {
00051 uint32_t size;
00052 double *elements;
00053 ts_window_function window_function;
00054 } x_dt_window;
00055
00056 #ifdef TEST_WINDOW
00057 static void print_window(dt_window w);
00058 #endif
00059
00060
00061 static double hamming_eq(int32_t num, int32_t den);
00062 static double hanning_eq(int32_t num, int32_t den);
00063 static double blackman_eq(int32_t num, int32_t den);
00064
00065 static const ts_window_function discrete_time_window_function_table[DT_NUMBER_OF_WINDOWS] = {
00066 {"Hamming", &hamming_eq},
00067 {"Hanning", &hanning_eq},
00068 {"Blackman", &blackman_eq}
00069 };
00070 #define NUMBER_OF_WINDOW_FUNCTIONS (sizeof(discrete_time_window_function_table) / sizeof(discrete_time_window_function_table[0]))
00071
00072
00073 static double* dt_window_compute(te_dt_window_type type, uint32_t size);
00074
00075 dt_window dt_window_create(te_dt_window_type type, uint32_t size)
00076 {
00077 dt_window window;
00078
00079 window = malloc(sizeof(x_dt_window));
00080
00081 if(window != NULL) {
00082 window->size = size;
00083 memcpy(
00084 (void*)&window->window_function,
00085 (void*)&discrete_time_window_function_table[type],
00086 sizeof(ts_window_function)
00087 );
00088 if(type < NUMBER_OF_WINDOW_FUNCTIONS) {
00089 window->elements = dt_window_compute(type, size);
00090 }
00091 }
00092 return window;
00093 }
00094
00095 void dt_window_apply(dt_window w, double *data)
00096 {
00097 uint32_t i;
00098
00099 if(w != NULL) {
00100 if(w->elements != NULL) {
00101 for(i=0; i < w->size; i++) {
00102 data[i] = data[i] * w->elements[i];
00103 }
00104 }
00105 }
00106 }
00107
00108 void dt_window_destroy(dt_window *w)
00109 {
00110 if(*w != NULL) {
00111 free((*w)->elements);
00112 }
00113 free(*w);
00114 *w = NULL;
00115 }
00116
00117 static double* dt_window_compute(te_dt_window_type type, uint32_t size)
00118 {
00119 uint32_t i, j;
00120 double *w;
00121
00122 if((w = malloc(size * sizeof(double))) != NULL) {
00123 if(size == 1) {
00124 w[0] = 1;
00125 }
00126 else {
00127 j = size - 1;
00128 for(i = 0; i < size; i++) {
00129 w[i] = discrete_time_window_function_table[type].function(i, j);
00130 }
00131 }
00132 }
00133 return w;
00134 }
00135
00136 static double hamming_eq(int32_t num, int32_t den)
00137 {
00138 return (0.54 - 0.46 * cos(2.0 * M_PI * (double)num / (double)den));
00139 }
00140
00141 static double hanning_eq(int32_t num, int32_t den)
00142 {
00143 return (0.5 - 0.5 * cos(2.0 * M_PI * (double)num / (double)den));
00144 }
00145
00146 static double blackman_eq(int32_t num, int32_t den)
00147 {
00148 return ( (0.42 - 0.5 * cos(2.0 * M_PI * (double)num / (double)den)) + (0.08 * cos(4.0 * M_PI * (double)num / (double)den)) );
00149 }
00150
00151 #ifdef TEST_WINDOW
00152 static void print_window(dt_window w)
00153 {
00154 int32_t i;
00155
00156 if(w != NULL) {
00157 if(w->elements != NULL) {
00158 printf("\n%s window function.\n", w->window_function.name);
00159 for(i=0; i < w->size; i++) {
00160 printf("w[%d] = %f\n", i, w->elements[i]);
00161 }
00162 }
00163 }
00164 }
00165
00166 int32_t main(int32_t argc, char *arg[])
00167 {
00168 te_dt_window_type i;
00169 uint32_t j;
00170 dt_window w;
00171
00172 if(arg[1] == NULL) {
00173 j = 8;
00174 }
00175 else {
00176 j = atoi(arg[1]);
00177 }
00178
00179 for(i = 0; i < DT_NUMBER_OF_WINDOWS; i++) {
00180 w = dt_window_create(i, j);
00181 print_window(w);
00182 dt_window_destroy(&w);
00183 }
00184
00185 return 0;
00186 }
00187 #endif