-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexample5.c
395 lines (214 loc) · 8.77 KB
/
example5.c
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
/* C O P Y R I G H T N O T I C E
*
* Copyright ©2021. Institute of Science and Technology Austria (IST Austria).
* All Rights Reserved. The underlying technology is protected by PCT Patent
* Application No. PCT/EP2021/054650.
*
* This file is part of the AP Demodulation library, which is free software: you can
* redistribute it and/or modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation in version 2.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY, without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License v2 for more details. You
* should have received a copy of the GNU General Public License v2 along with this
* program. If not, see https://www.gnu.org/licenses/.
*
* Contact the Technology Transfer Office, IST Austria, Am Campus 1,
* A-3400 Klosterneuburg, Austria, +43-(0)2243 9000, twist@ist.ac.at, for commercial
* licensing opportunities.
*
* See https://github.com/mgabriel-lt/ap-demodulation for the latest version of the
* code and user-friendly explanations on the working principle, domains of
* application, and advice on the usage of different AP Demodulation algorithms in
* practice.
*/
/* EXAMPLE 5
*
* In this example, a synthetic 1D amplitude-modulated signal built from a
* regular-spikes carrier and two LP-random modulators is generated and demodulated
* by using the AP-Basic algorithm. The two modulators are used to shape the lower
* and upper envelopes of the signal. Sample points of the the predefined and
* inferred modulators and carriers are then written into a text file for further
* analysis. This example illustrates how to infer the upper and lower envelopes of a
* signal by using the function 'f_apd_demodulation'.
*
* Compile this program by using Option 1 described in the documentation.
*/
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "f_apd_demodulation.c"
#ifdef _WIN32
#define STR_NL "\r"
#else
#define STR_NL "\n"
#endif
int main(void)
{
/* File pointer */
FILE *fid;
/* Exit flag */
int exitflag = 0;
/* Sets f_apd_demodulation to return control to the calling f-tion upon error */
f_apd_set_errexit(0);
/* Iteration variables */
long i;
long j;
long iter;
/* Number of sample points */
long n = 1024;
/* Time step */
double dt = (double)10/(n-1);
/* Modulators (low-pass-random signals) */
double w[] = {1.5648, 0.5312, 0.1413, 0.7588, -0.8616, -0.3586, 0.9106, -0.1787,\
-0.0108, -0.0989, -0.3559, -0.4015, 0.2917, -0.3458, -1.1990, \
0.7651, -0.9884, -1.1668, 0.6584, -1.3693, 0.7608, 0.7810, 0.9041,\
0.2338, 0.1767, 0.3911, 0.3206, 0.8155, 0.6135, 0.7600};
double *m1 = (double*) calloc(n, sizeof(double));
for (i=0; i<n; i++)
{
for (j=0; j<10; j++)
m1[i] = m1[i] + w[2*j] * cos(2*M_PI*j*i/(double)n+w[1+2*j]);
m1[i] = m1[i] + 2.130185657756246;
m1[i] = m1[i] / 7.925671964919291;
}
double *m2 = (double*) calloc(n, sizeof(double));
for (i=0; i<n; i++)
{
for (j=14; j>=0; j--)
m2[i] = m2[i] + w[2*j] * cos(2*M_PI*j*(n-i)/(double)n+w[1+2*j]);
m2[i] = m2[i] + 2.581418146550079;
m2[i] = m2[i] / 8.690964954126397 / 2;
}
/* Carrier (a regular-spikes signal) */
double *c1 = (double*) calloc(n, sizeof(double));
double *c2 = (double*) calloc(n, sizeof(double));
for (i=4-1; i<n; i+=32)
{
c1[i] = 1;
c2[i-2] = -1;
c2[i+2] = -1;
}
/* Signal */
double *s = (double*) malloc(n*sizeof(double));
for (i=0; i<n; i++)
s[i] = m1[i] * c1[i] + m2[i] * c2[i];
/* Minimum elements of the signal and the negative signal */
double smin = s[0];
double smmin = -s[0];
for (i=1; i<n; i++)
{
if (s[i] < smin)
smin = s[i];
else if (-s[i] < smmin)
smmin = -s[i];
}
/* Demodulation parameters */
struct strAPD_Par Par;
Par.Al = 'B'; // algorithm ('B'-> AP-Basic)
Par.D = 1; // dimension of the signal
Par.Fs[0] = 1/dt; // sampling frequency
Par.Fc[0] = 15*Par.Fs[0]/n; // cutoff frequency
Par.Et = 1e-6; // infeasibility error tolerance
Par.Ni = 1e+3; // maximum number of iterations
Par.Ns[0] = n; // number of sample points
Par.Cp = 1; // compression parameter (=1->no compression)
Par.im = (long*) malloc(2*sizeof(long)); // iterations to save modulator
// estimates at; here, we ask for
Par.im[0] = 1; // estimate at the last iteration only
Par.im[1] = Par.Ni;
Par.ie = (long*) malloc(2*sizeof(long)); // iterations to save infeasibility
// error estimates at; here, we ask for
Par.ie[0] = 1; // estimate at the last iteration only
Par.ie[1] = Par.Ni;
/* Upper bound on the modulator */
double *Ub = NULL; // we assume no upper bound on the modulator
/* Sampling coordinates of the input signal */
double *t = NULL; // we use no interpolation
/* Output arrays */
double *out_m1 = (double*) malloc(Par.im[0]*n*sizeof(double));
double *out_m2 = (double*) malloc(Par.im[0]*n*sizeof(double));
double *out_e1 = (double*) malloc(Par.ie[0]*sizeof(double));
double *out_e2 = (double*) malloc(Par.ie[0]*sizeof(double));
/* Demodulation (lower envelope) */
for (i=0; i<n; i++)
s[i] = -s[i] - smmin;
exitflag = f_apd_demodulation (s, &Par, Ub, t, out_m2, out_e2, &iter);
if (exitflag != 0)
goto failed;
else
{
printf(STR_NL "Demodulation for estimating the lower envelope completed." \
STR_NL);
printf(STR_NL "The infeasibility error is %e. " STR_NL, out_e2[0]);
printf(STR_NL "The number of used iterations is %ld" STR_NL STR_NL, iter);
}
for (i=0; i<n; i++)
{
out_m2[i] = -out_m2[i] - smmin;
s[i] = -s[i] - smmin - smin;
}
/* Demodulation (upper envelope) */
exitflag = f_apd_demodulation (s, &Par, Ub, t, out_m1, out_e1, &iter);
if (exitflag != 0)
goto failed;
else
{
printf(STR_NL "Demodulation for estimating the upper envelope completed." \
STR_NL);
printf(STR_NL "The infeasibility error is %e. " STR_NL, out_e1[0]);
printf(STR_NL "The number of used iterations is %ld" STR_NL STR_NL, iter);
}
for (i=0; i<n; i++)
{
out_m1[i] = out_m1[i] + smin;
s[i] = s[i] + smin;
}
/* Output. Values of the signal as well as predefined and estimated envelopes
* are written to a text file as tab-delimited columns. */
fid = fopen("./out_ex5.txt", "w");
if (fid == NULL)
{
exitflag = -1;
goto failed;
}
else
{
fprintf(fid, "Sig.\t Mod. u.\t Mod. l.\t Mod. u. est.\t Mod. l. est." STR_NL);
for (i=0; i<n; i++)
fprintf(fid, "%e\t%e\t%e\t%e\t%e" STR_NL, s[i], m1[i], -m2[i], out_m1[i], \
out_m2[i]);
exitflag = fclose(fid);
if (exitflag != 0)
{
exitflag = -2;
goto failed;
}
}
printf(STR_NL "Results saved." STR_NL STR_NL);
/* Output & Memory deallocation */
finish:
free(m1);
free(m2);
free(c1);
free(c2);
free(s);
free(Par.im);
free(Par.ie);
free(out_m1);
free(out_m2);
free(out_e1);
free(out_e2);
return exitflag;
failed:
if (exitflag == -1)
fprintf (stderr, STR_NL "Error in example1.c: out_ex1.txt could not "\
"be opened for writing!" STR_NL STR_NL);
else if (exitflag == -2)
fprintf (stderr, STR_NL "Error in example1.c: stream to out_ex1.txt "\
"could not be closed!" STR_NL STR_NL);
else
f_apd_print_error(exitflag);
goto finish;
}