annotate Meerwald/dwt.c @ 22:d8551fb39a5e default tip

Added tag v0.6 for changeset 1c4ccd635a68
author Peter Meerwald-Stadler <pmeerw@pmeerw.net>
date Sat, 28 Jan 2023 23:57:51 +0100
parents 1906e659edd0
children
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:
14
1906e659edd0 fix undef. behaviour
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 8
diff changeset
159 filter[k] = (double) (((i & 0x01) * 2) - 1) * tf[i] / 2.0;
1906e659edd0 fix undef. behaviour
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 8
diff changeset
160 i++;
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
161 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
162 case FILTERHi:
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
163 filter[k] = tf[j--];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
164 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
165 case FILTERGi:
14
1906e659edd0 fix undef. behaviour
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 8
diff changeset
166 filter[k] = (double) (((i & 0x01) * 2) - 1) * tf[i];
1906e659edd0 fix undef. behaviour
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 8
diff changeset
167 i++;
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
168 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
169 default:
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
170 return -1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
171 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
172
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
173 while (k < 6)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
174 filter[k++] = 0.0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
175
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
176 return filterlength;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
177 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
178
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
179 void dwt_pollen_filter(double alpha, double beta) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
180 FilterGH filter;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
181 int i;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
182
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
183 filter = malloc(sizeof(struct FilterGHStruct));
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
184 #ifdef DEBUG
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
185 if (!filter) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
186 fprintf(stderr, "dwt_pollen_filter(): malloc failed()\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
187 return;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
188 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
189 #endif
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
190
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
191 filter->type = FTOther;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
192 filter->name = "pollen";
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
193
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
194 filter->g = new_filter(6);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
195 filter->g->type = FTSymm;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
196 filter->g->hipass = 1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
197 filter->g->len = gen_pollen_filter(filter->g->data, alpha, beta, FILTERG);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
198 filter->g->start = -filter->g->len / 2;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
199 filter->g->end = filter->g->len / 2 - 1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
200
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
201 filter->h = new_filter(6);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
202 filter->h->type = FTSymm;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
203 filter->h->hipass = 0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
204 filter->h->len = gen_pollen_filter(filter->h->data, alpha, beta, FILTERH);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
205 filter->h->start = -filter->h->len / 2;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
206 filter->h->end = filter->h->len / 2 - 1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
207
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
208 filter->gi = new_filter(6);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
209 filter->gi->type = FTSymm;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
210 filter->gi->hipass = 1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
211 filter->gi->len = gen_pollen_filter(filter->gi->data, alpha, beta, FILTERGi);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
212 filter->gi->start = -filter->gi->len / 2;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
213 filter->gi->end = filter->gi->len / 2 - 1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
214
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
215 filter->hi = new_filter(6);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
216 filter->hi->type = FTSymm;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
217 filter->hi->hipass = 0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
218 filter->hi->len = gen_pollen_filter(filter->hi->data, alpha, beta, FILTERHi);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
219 filter->hi->start = -filter->hi->len / 2;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
220 filter->hi->end = filter->hi->len / 2 - 1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
221
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
222 #ifdef DEBUG
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
223 if (dwt_levels <= 0) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
224 fprintf(stderr, "dwt_pollen_filter(): level invalid - set to zero\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
225 return;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
226 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
227 #endif
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
228
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
229 #ifdef DEBUG
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
230 if (!dwt_filters) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
231 fprintf(stderr, "dwt_pollen_filter(): wm_dwt not initialized, call init_dwt() first\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
232 return;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
233 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
234 #endif
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
235
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
236 for (i = 0; i < dwt_levels + 1; i++)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
237 dwt_filters[i] = filter;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
238 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
239
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
240 int gen_param_filter(double *filter, int n, double alpha[], int which) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
241 int i, j, k, filterlength;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
242 double *tf, *t;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
243
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
244 tf = malloc(2 * (n + 1) * sizeof(double));
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
245 t = malloc(2 * (n + 1) * sizeof(double));
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
246 if (!tf) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
247 fprintf(stderr, "gen_param_filter(): malloc() failed\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
248 return -1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
249 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
250
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
251 tf[0] = 1.0 / sqrt(2.0);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
252 tf[1] = 1.0 / sqrt(2.0);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
253
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
254 for (k = 0; k < n; k++) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
255 for (i = 0; i < 2 * (k + 2); i++) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
256
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
257 #define H(X) (((X) < 0 || (X) >= 2 * (k + 1)) ? 0.0 : tf[X])
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
258
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
259 t[i] = 0.5 * (H(i - 2) + H(i) +
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
260 cos(alpha[k]) * (H(i - 2) - H(i)) +
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
261 (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
262 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
263 for (i = 0; i < 2 * (k + 2); i++) tf[i] = t[i];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
264 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
265
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
266 /* set close-to-zero filter coefficients to zero */
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
267 for (i = 0; i < 2 * (n + 1) ; i++)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
268 if (fabs(tf[i]) < 1.0e-15) tf[i] = 0.0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
269
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
270 /* find the first non-zero wavelet coefficient */
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
271 i = 0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
272 while (tf[i] == 0.0) i++;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
273
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
274 /* find the last non-zero wavelet coefficient */
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
275 j = 2 * (n + 1) - 1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
276 while (tf[j] == 0.0) j--;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
277
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
278 filterlength = j - i + 1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
279 for (k = 0; k < filterlength; k++)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
280 switch (which) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
281 case FILTERG:
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
282 case FILTERGi:
14
1906e659edd0 fix undef. behaviour
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 8
diff changeset
283 filter[k] = (double) ((((i+1) & 0x01) * 2) - 1) * tf[i];
1906e659edd0 fix undef. behaviour
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 8
diff changeset
284 i++;
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
285 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
286 case FILTERH:
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
287 case FILTERHi:
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
288 filter[k] = tf[j--];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
289 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
290 default:
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
291 return -1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
292 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
293
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
294 while (k < 2 * (n + 1))
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
295 filter[k++] = 0.0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
296
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
297 return filterlength;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
298 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
299
3
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
300 void dwt_param_filter(double alpha[], int param_len[]) {
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
301 FilterGH filter;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
302 int i;
3
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
303 int param_len_sum = 0;
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
304
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
305 #ifdef DEBUG
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
306 if (dwt_levels <= 0) {
3
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
307 fprintf(stderr, "dwt_param_filter(): level invalid - set to zero\n");
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
308 return;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
309 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
310 #endif
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
311
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
312 #ifdef DEBUG
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
313 if (!dwt_filters) {
3
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
314 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
315 return;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
316 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
317 #endif
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
318
3
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
319
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
320 for (i = 0; i < dwt_levels + 1; i++) {
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
321
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
322 filter = malloc(sizeof(struct FilterGHStruct));
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
323 #ifdef DEBUG
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
324 if (!filter) {
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
325 fprintf(stderr, "dwt_param_filter(): malloc failed()\n");
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
326 return;
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
327 }
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
328 #endif
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->type = FTOrtho;
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
331 filter->name = "param";
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
332
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
333 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
334 filter->g->type = FTSymm;
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
335 filter->g->hipass = 1;
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
336 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
337 param_len[i], &alpha[param_len_sum],
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
338 FILTERG);
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
339 filter->g->start = -filter->g->len / 2;
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
340 filter->g->end = filter->g->len / 2 - 1;
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
341
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
342 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
343 filter->h->type = FTSymm;
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
344 filter->h->hipass = 0;
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
345 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
346 param_len[i], &alpha[param_len_sum],
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
347 FILTERH);
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
348 filter->h->start = -filter->h->len / 2;
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
349 filter->h->end = filter->h->len / 2 - 1;
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
350
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
351 filter->gi = 0;
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
352 filter->hi = 0;
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
353
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
354 dwt_filters[i] = filter;
3
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
355
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
356 param_len_sum += param_len[i];
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
357 }
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
358 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
359
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
360 void done_dwt() {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
361 }

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