annotate Meerwald/dwt.c @ 8:f83ef905a63d

fixing many warnings increase capacity for coordinates in bruyn fix some uninit. variables
author Peter Meerwald <pmeerw@cosy.sbg.ac.at>
date Tue, 22 Apr 2008 13:36:05 +0200
parents acb6967ee76d
children 1906e659edd0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
1 #include "wm.h"
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
2 #include "dwt.h"
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
3
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
4 char filter_file[MAXPATHLEN] = "";
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
5 AllFilters dwt_allfilters;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
6 FilterGH *dwt_filters = NULL;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
7 int dwt_method;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
8 int dwt_cols;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
9 int dwt_rows;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
10 int dwt_levels;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
11 int dwt_filter;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
12
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
13 void init_dwt(int cols, int rows, const char *filter_name, int filter, int level, int method) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
14 int i;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
15
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
16 if (strcmp(filter_file, filter_name)) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
17 if (filter_name)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
18 strcpy(filter_file, filter_name);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
19 else
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
20 strcpy(filter_file, "filter.dat");
3
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
21
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
22 /* memory leak here - there is no function unload_filters() */
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
23 dwt_allfilters = load_filters(filter_file);
3
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
24
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
25 if (!dwt_allfilters) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
26 fprintf(stderr, "init_dwt(): unable to open filter definition file %s\n", filter_file);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
27 return;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
28 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
29 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
30
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
31 #ifdef DEBUG
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
32 if (level <= 0 || level > rint(log(MIN(cols, rows))/log(2.0)) - 2) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
33 fprintf(stderr, "init_dwt(): level parameter does not match image width/height\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
34 return;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
35 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
36 #endif
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
37
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
38 if (dwt_filters && level != dwt_levels) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
39 free(dwt_filters);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
40 dwt_filters = NULL;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
41 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
42
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
43 dwt_levels = level;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
44
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
45 if (!dwt_filters)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
46 dwt_filters = calloc(level + 1, sizeof(FilterGH));
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
47
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
48 for (i = 0; i < level + 1; i++)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
49 dwt_filters[i] = (dwt_allfilters->filter)[filter];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
50
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
51 dwt_filter = filter;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
52 dwt_method = method;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
53 dwt_cols = cols;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
54 dwt_rows = rows;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
55 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
56
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
57 Image_tree fdwt(gray **pixels) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
58 Image image;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
59 Image_tree tree;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
60 int i, j;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
61
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
62 image = new_image(dwt_cols, dwt_rows);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
63
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
64 for (i = 0; i < dwt_rows; i++)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
65 for (j = 0; j < dwt_cols; j++)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
66 set_pixel(image, j, i, pixels[i][j]);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
67
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
68 tree = wavelettransform(image, dwt_levels, dwt_filters, dwt_method);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
69 free_image(image);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
70
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
71 return tree;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
72 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
73
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
74 Image_tree fdwt_wp(gray **pixels) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
75 Image image;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
76 Image_tree tree;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
77 int i, j;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
78
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
79 image = new_image(dwt_cols, dwt_rows);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
80
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
81 for (i = 0; i < dwt_rows; i++)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
82 for (j = 0; j < dwt_cols; j++)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
83 set_pixel(image, j, i, pixels[i][j]);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
84
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
85 tree = wavelettransform_wp(image, dwt_levels, dwt_filters, dwt_method);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
86 free_image(image);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
87
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
88 return tree;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
89 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
90
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
91 void idwt(Image_tree dwts, gray **pixels) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
92 Image image;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
93 int i, j;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
94
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
95 image = inv_transform(dwts, dwt_filters, dwt_method + 1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
96
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
97 for (i = 0; i < dwt_rows; i++)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
98 for (j = 0; j < dwt_cols; j++)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
99 pixels[i][j] = PIXELRANGE((int) (get_pixel(image, j, i) + 0.5));
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
100
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
101 free_image(image);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
102 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
103
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
104 void idwt_wp(Image_tree dwts, gray **pixels) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
105 Image image;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
106 int i, j;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
107
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
108 image = inv_transform(dwts, dwt_filters, dwt_method + 1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
109
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
110 for (i = 0; i < dwt_rows; i++)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
111 for (j = 0; j < dwt_cols; j++)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
112 pixels[i][j] = PIXELRANGE((int) (get_pixel(image, j, i) + 0.5));
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
113
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
114 free_image(image);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
115 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
116
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
117 int gen_pollen_filter(double *filter, double alpha, double beta, int which) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
118 int i, j, k, filterlength;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
119 double tf[6];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
120
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
121 /* parameter alpha, beta have to be in range -Pi .. Pi */
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
122 if (alpha < -M_PI || alpha >= M_PI) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
123 fprintf(stderr, "alpha %f out of range\n", alpha);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
124 return -1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
125 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
126
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
127 if (beta < -M_PI || beta >= M_PI) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
128 fprintf(stderr, "beta %f out of range\n", beta);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
129 return -1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
130 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
131
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
132 /* generate Pollen filter coefficients, see http://www.dfw.net/~cody for details */
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
133 tf[0] = ((1.0 + cos(alpha) + sin(alpha)) * (1.0 - cos(beta) - sin(beta)) + 2.0 * sin(beta) * cos(alpha)) / 4.0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
134 tf[1] = ((1.0 - cos(alpha) + sin(alpha)) * (1.0 + cos(beta) - sin(beta)) - 2.0 * sin(beta) * cos(alpha)) / 4.0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
135 tf[2] = (1.0 + cos(alpha - beta) + sin(alpha - beta)) / 2.0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
136 tf[3] = (1.0 + cos(alpha - beta) - sin(alpha - beta)) / 2.0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
137 tf[4] = 1.0 - tf[0] - tf[2];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
138 tf[5] = 1.0 - tf[1] - tf[3];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
139
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
140 /* set close-to-zero filter coefficients to zero */
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
141 for (i = 0; i < 6; i++)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
142 if (fabs(tf[i]) < 1.0e-15) tf[i] = 0.0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
143
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
144 /* find the first non-zero wavelet coefficient */
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
145 i = 0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
146 while (tf[i] == 0.0) i++;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
147
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
148 /* find the last non-zero wavelet coefficient */
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
149 j = 5;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
150 while (tf[j] == 0.0) j--;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
151
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
152 filterlength = j - i + 1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
153 for (k = 0; k < filterlength; k++)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
154 switch (which) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
155 case FILTERH:
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
156 filter[k] = tf[j--] / 2.0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
157 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
158 case FILTERG:
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
159 filter[k] = (double) (((i & 0x01) * 2) - 1) * tf[i++] / 2.0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
160 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
161 case FILTERHi:
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
162 filter[k] = tf[j--];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
163 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
164 case FILTERGi:
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
165 filter[k] = (double) (((i & 0x01) * 2) - 1) * tf[i++];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
166 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
167 default:
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
168 return -1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
169 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
170
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
171 while (k < 6)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
172 filter[k++] = 0.0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
173
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
174 return filterlength;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
175 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
176
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
177 void dwt_pollen_filter(double alpha, double beta) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
178 FilterGH filter;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
179 int i;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
180
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
181 filter = malloc(sizeof(struct FilterGHStruct));
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
182 #ifdef DEBUG
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
183 if (!filter) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
184 fprintf(stderr, "dwt_pollen_filter(): malloc failed()\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
185 return;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
186 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
187 #endif
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
188
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
189 filter->type = FTOther;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
190 filter->name = "pollen";
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
191
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
192 filter->g = new_filter(6);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
193 filter->g->type = FTSymm;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
194 filter->g->hipass = 1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
195 filter->g->len = gen_pollen_filter(filter->g->data, alpha, beta, FILTERG);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
196 filter->g->start = -filter->g->len / 2;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
197 filter->g->end = filter->g->len / 2 - 1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
198
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
199 filter->h = new_filter(6);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
200 filter->h->type = FTSymm;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
201 filter->h->hipass = 0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
202 filter->h->len = gen_pollen_filter(filter->h->data, alpha, beta, FILTERH);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
203 filter->h->start = -filter->h->len / 2;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
204 filter->h->end = filter->h->len / 2 - 1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
205
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
206 filter->gi = new_filter(6);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
207 filter->gi->type = FTSymm;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
208 filter->gi->hipass = 1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
209 filter->gi->len = gen_pollen_filter(filter->gi->data, alpha, beta, FILTERGi);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
210 filter->gi->start = -filter->gi->len / 2;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
211 filter->gi->end = filter->gi->len / 2 - 1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
212
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
213 filter->hi = new_filter(6);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
214 filter->hi->type = FTSymm;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
215 filter->hi->hipass = 0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
216 filter->hi->len = gen_pollen_filter(filter->hi->data, alpha, beta, FILTERHi);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
217 filter->hi->start = -filter->hi->len / 2;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
218 filter->hi->end = filter->hi->len / 2 - 1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
219
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
220 #ifdef DEBUG
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
221 if (dwt_levels <= 0) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
222 fprintf(stderr, "dwt_pollen_filter(): level invalid - set to zero\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
223 return;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
224 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
225 #endif
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
226
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
227 #ifdef DEBUG
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
228 if (!dwt_filters) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
229 fprintf(stderr, "dwt_pollen_filter(): wm_dwt not initialized, call init_dwt() first\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
230 return;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
231 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
232 #endif
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
233
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
234 for (i = 0; i < dwt_levels + 1; i++)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
235 dwt_filters[i] = filter;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
236 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
237
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
238 int gen_param_filter(double *filter, int n, double alpha[], int which) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
239 int i, j, k, filterlength;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
240 double *tf, *t;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
241
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
242 tf = malloc(2 * (n + 1) * sizeof(double));
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
243 t = malloc(2 * (n + 1) * sizeof(double));
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
244 if (!tf) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
245 fprintf(stderr, "gen_param_filter(): malloc() failed\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
246 return -1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
247 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
248
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
249 tf[0] = 1.0 / sqrt(2.0);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
250 tf[1] = 1.0 / sqrt(2.0);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
251
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
252 for (k = 0; k < n; k++) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
253 for (i = 0; i < 2 * (k + 2); i++) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
254
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
255 #define H(X) (((X) < 0 || (X) >= 2 * (k + 1)) ? 0.0 : tf[X])
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
256
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
257 t[i] = 0.5 * (H(i - 2) + H(i) +
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
258 cos(alpha[k]) * (H(i - 2) - H(i)) +
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
259 (i & 1 ? -1.0 : 1.0) * sin(alpha[k]) * (H(2 * (k + 2) - i - 1) - H(2 * (k + 2) - i - 3)));
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
260 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
261 for (i = 0; i < 2 * (k + 2); i++) tf[i] = t[i];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
262 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
263
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
264 /* set close-to-zero filter coefficients to zero */
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
265 for (i = 0; i < 2 * (n + 1) ; i++)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
266 if (fabs(tf[i]) < 1.0e-15) tf[i] = 0.0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
267
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
268 /* find the first non-zero wavelet coefficient */
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
269 i = 0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
270 while (tf[i] == 0.0) i++;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
271
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
272 /* find the last non-zero wavelet coefficient */
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
273 j = 2 * (n + 1) - 1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
274 while (tf[j] == 0.0) j--;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
275
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
276 filterlength = j - i + 1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
277 for (k = 0; k < filterlength; k++)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
278 switch (which) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
279 case FILTERG:
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
280 case FILTERGi:
8
f83ef905a63d fixing many warnings
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 3
diff changeset
281 filter[k] = (double) ((((i+1) & 0x01) * 2) - 1) * tf[i++];
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
282 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
283 case FILTERH:
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
284 case FILTERHi:
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
285 filter[k] = tf[j--];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
286 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
287 default:
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
288 return -1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
289 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
290
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
291 while (k < 2 * (n + 1))
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
292 filter[k++] = 0.0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
293
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
294 return filterlength;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
295 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
296
3
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
297 void dwt_param_filter(double alpha[], int param_len[]) {
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
298 FilterGH filter;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
299 int i;
3
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
300 int param_len_sum = 0;
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
301
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
302 #ifdef DEBUG
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
303 if (dwt_levels <= 0) {
3
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
304 fprintf(stderr, "dwt_param_filter(): level invalid - set to zero\n");
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
305 return;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
306 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
307 #endif
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
308
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
309 #ifdef DEBUG
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
310 if (!dwt_filters) {
3
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
311 fprintf(stderr, "dwt_param_filter(): wm_dwt not initialized, call init_dwt() first\n");
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
312 return;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
313 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
314 #endif
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
315
3
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
316
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
317 for (i = 0; i < dwt_levels + 1; i++) {
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
318
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
319 filter = malloc(sizeof(struct FilterGHStruct));
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
320 #ifdef DEBUG
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
321 if (!filter) {
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
322 fprintf(stderr, "dwt_param_filter(): malloc failed()\n");
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
323 return;
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
324 }
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
325 #endif
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
326
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
327 filter->type = FTOrtho;
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
328 filter->name = "param";
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
329
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
330 filter->g = new_filter(2 * (param_len[i] + 1));
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
331 filter->g->type = FTSymm;
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
332 filter->g->hipass = 1;
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
333 filter->g->len = gen_param_filter(filter->g->data,
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
334 param_len[i], &alpha[param_len_sum],
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
335 FILTERG);
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
336 filter->g->start = -filter->g->len / 2;
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
337 filter->g->end = filter->g->len / 2 - 1;
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
338
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
339 filter->h = new_filter(2 * (param_len[i] + 1));
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
340 filter->h->type = FTSymm;
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
341 filter->h->hipass = 0;
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
342 filter->h->len = gen_param_filter(filter->h->data,
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
343 param_len[i], &alpha[param_len_sum],
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
344 FILTERH);
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
345 filter->h->start = -filter->h->len / 2;
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
346 filter->h->end = filter->h->len / 2 - 1;
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
347
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
348 filter->gi = 0;
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
349 filter->hi = 0;
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
350
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
351 dwt_filters[i] = filter;
3
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
352
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
353 param_len_sum += param_len[i];
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
354 }
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
355 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
356
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
357 void done_dwt() {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
358 }

Repositories maintained by Peter Meerwald, pmeerw@pmeerw.net.