-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoly.c
2478 lines (2197 loc) · 77 KB
/
poly.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
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
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* poly.c - Polynomial (de)compression routines
*
* Copyright (c) 2015 Maurizio Tomasi
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "libpolycomp.h"
#include <assert.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <gsl/gsl_block_double.h>
#include <gsl/gsl_vector_double.h>
#include <gsl/gsl_matrix_double.h>
#include <gsl/gsl_multifit.h>
#include <fftw3.h>
#ifdef WITH_OPENMP
#include <omp.h>
#else
static int omp_get_thread_num(void) { return 0; }
static int omp_get_max_threads(void) { return 1; }
#endif
/**********************************************************************/
/** \defgroup poly Polynomial compression functions
*
* Polynomial compression relies on a simple idea, that is to divide
* the input data stream into subsets of consecutive samples (called
* "chunks"), and to approximate each chunk by means of a polynomial.
* Such compression is inherently lossy, as the residuals of the
* fitting procedure are usually discarded. If the polynomial used for
* the fitting produces residuals that are too large, usually the
* samples in the chunk are saved in uncompressed form.
*
* This idea has been widely applied in the literature. Libpolycomp
* implements an improvement over it, because if the fit residuals are
* too large, the library saves a chopped sequence of the Chebyshev
* transform of the residuals. This allows to achieve better
* compression ratios in those cases where polynomial fitting is not
* always enough to keep compression errors below the desired
* threshold. This kind of compression works quite well for smooth
* data series, where changes between consecutive samples are well
* described by slowly varying continuous functions. It is not
* suitable if the signal contains noise, unless this noise is
* significantly smaller than the signal and than the error threshold.
*
* Libpolycomp allows to avoid the usage of Chebyshev transforms. In
* this case, if no polynomial of the desired degree are able to fit
* the data with the given error threshold, the data for that chunk is
* saved uncompressed.
*
* The typical workflow for applying polynomial compression is the
* following:
*
* 1. Allocate a new \ref pcomp_polycomp_t object via a call to \ref
* pcomp_init_polycomp. Such object contains the parameters to be
* used for the compression, e.g., the size of each chunk, the
* degree of the fitting polynomial, whether to apply or not the
* Chebyshev transform to the residuals, etc.
*
* 2. Split the data into chunks and compress each of them using the
* function \ref pcomp_compress_polycomp.
*
* 3. Convert the list of chunks into a byte sequence using \ref
* pcomp_encode_chunks, typically with the purpose of saving it
* into a file or sending it through a pipe/socket/etc.
*
* The decompression workflow is specular:
*
* 1. Process the byte sequence containing the compressed data using
* \ref pcomp_decode_chunks. This will produce a list of chunks
* that are still compressed.
*
* 2. Decompress the chunks using the function \ref
* pcomp_decompress_polycomp.
*
* The compression functions described in this page use the \ref
* pcomp_polycomp_t structure to determine which parameters to use for
* the compression. The functions that allow to allocate/free/manage
* this structure are the following:
*
* - \ref pcomp_init_polycomp and \ref pcomp_free_polycomp
* - \ref pcomp_polycomp_samples_per_chunk
* - \ref pcomp_polycomp_num_of_poly_coeffs
* - \ref pcomp_polycomp_max_error
* - \ref pcomp_polycomp_algorithm
* - \ref pcomp_polycomp_period and \ref pcomp_polycomp_set_period
*
* It is possible to use a set of more low-level functions to use
* polynomial compression. Refer to \ref poly_lowlevel for further
* information.
*/
/** \defgroup poly_lowlevel Polynomial compression (low-level functions)
*/
/**********************************************************************/
static double integer_power(int x, int y)
{
double dbl_x = (double)x;
if (y < 0)
abort();
if (y == 0)
return 1.0;
else if (y == 1)
return dbl_x;
else {
double result = dbl_x * dbl_x;
int cur_power = 2;
while (2 * cur_power < y) {
result *= result;
cur_power *= 2;
}
if (y > cur_power)
result *= integer_power(x, y - cur_power);
return result;
}
}
/***********************************************************************
* Types and functions used for polynomial least-square fitting
*/
struct __pcomp_poly_fit_data_t {
size_t num_of_samples;
size_t num_of_coeffs;
gsl_multifit_linear_workspace* workspace;
gsl_matrix* matrix;
gsl_vector* y;
gsl_vector* c;
gsl_matrix* cov_matrix;
};
/** \ingroup polyfit
*
* \brief Allocate a new instance of the \ref pcomp_poly_fit_data_t
* structure on the heap
*
* \param[in] num_of_samples Number of floating-point numbers that
* must fit the polynomial
*
* \param[in] num_of_coeffs Number of coefficients of the
* least-squares fitting polynomial \f$p(x)\f$. This is equal to
* \f$\deg p(x) + 1\f$, where \f$\deg p(x)\f$ is the degree of the
* polynomial. Thus, for a parabolic polynomial of the form \f$p(x) =
* a x^2 + b x + c\f$, \a num_of_coeffs = 3.
*
* \returns A newly created instance of \ref pcomp_poly_fit_data_t
* structure. This must be freed using \ref pcomp_free_poly_fit, once
* it is no longer used.
*/
pcomp_poly_fit_data_t* pcomp_init_poly_fit(size_t num_of_samples,
size_t num_of_coeffs)
{
size_t i, j;
pcomp_poly_fit_data_t* poly_fit
= malloc(sizeof(pcomp_poly_fit_data_t));
if (poly_fit == NULL)
abort();
poly_fit->num_of_samples = num_of_samples;
poly_fit->num_of_coeffs = num_of_coeffs;
poly_fit->workspace
= gsl_multifit_linear_alloc(num_of_samples, num_of_coeffs);
poly_fit->matrix = gsl_matrix_alloc(num_of_samples, num_of_coeffs);
poly_fit->y = gsl_vector_alloc(num_of_samples);
poly_fit->c = gsl_vector_alloc(num_of_coeffs);
poly_fit->cov_matrix
= gsl_matrix_alloc(num_of_coeffs, num_of_coeffs);
for (i = 0; i < num_of_samples; ++i) {
for (j = 0; j < num_of_coeffs; ++j) {
gsl_matrix_set(poly_fit->matrix, i, j,
integer_power(i + 1, j));
}
}
return poly_fit;
}
/** \ingroup polyfit
*
* \brief Free an instance of the \ref pcomp_poly_fit_data_t that has
* been allocated via a call to \ref pcomp_init_poly_fit.
*
* \param[in] poly_fit Pointer to the structure to be freed
*/
void pcomp_free_poly_fit(pcomp_poly_fit_data_t* poly_fit)
{
if (poly_fit == NULL)
return;
gsl_matrix_free(poly_fit->matrix);
gsl_vector_free(poly_fit->y);
gsl_vector_free(poly_fit->c);
gsl_matrix_free(poly_fit->cov_matrix);
gsl_multifit_linear_free(poly_fit->workspace);
free(poly_fit);
}
/** \ingroup polyfit
*
* \brief Return the number of samples to be used in a polynomial fit
*
* \param[in] poly_fit Pointer to the structure detailing the fit
*
* \returns The number of samples that should be passed to a call to
* \ref pcomp_run_poly_fit.
*/
size_t
pcomp_poly_fit_num_of_samples(const pcomp_poly_fit_data_t* poly_fit)
{
if (poly_fit == NULL)
abort();
return poly_fit->num_of_samples;
}
/** \ingroup polyfit
*
* \brief Return the number of coefficients of the least-squares
* fitting polynomial
*
* \param[in] poly_fit Pointer to the structure detailing the fit
*
* \returns The number of coefficients for the fitting polynomial (one
* plus the polynomial degree)
*/
size_t
pcomp_poly_fit_num_of_coeffs(const pcomp_poly_fit_data_t* poly_fit)
{
if (poly_fit == NULL)
abort();
return poly_fit->num_of_coeffs;
}
/** \ingroup polyfit
*
* \brief Calculates a polynomial least-squares fit.
*
* Compute a least-squares fit between the numbers \f$x_i\f$ (with
* \f$i = 1 \ldots N\f$) and the polynomial \f$p(x)\f$ through the
* points \f$(i, x_i)_{i=1}^N\f$. The coefficients of \f$p(x)\f$ are
* saved in \a coeffs, from the least to the greatest degree.
*
* Here is an example of the usage of this function:
*
* \code{.c}
* double points[] = { 1.0, 3.0, 5.0 };
* double coeffs[2];
* const size_t num_of_points = sizeof(points) / sizeof(points[0]);
* const size_t num_of_coeffs = sizeof(coeffs) / sizeof(coeffs[0]);
* pcomp_poly_fit_data_t* poly_fit;
*
* poly_fit = pcomp_init_poly_fit(num_of_points, num_of_coeffs);
* pcomp_run_poly_fit(poly_fit, coeffs, points);
* printf("The data are fitted by the polynomial y = %f + %f x\n",
* coeffs[0], coeffs[1]);
* \endcode
*
* \param[in] poly_fit Pointer to a \ref pcomp_poly_fit_data_t
* structure, created using the \ref pcomp_init_poly_fit function.
*
* \param[out] coeffs Pointer to an array where the coefficients of
* the polynomial will be stored on exit. The array must have room for
* a number of elements greater or equal than the value returned by
* \ref pcomp_poly_fit_num_of_coeffs.
*
* \param[in] points Array of numbers \f$x_i\f$ to use in the fit. The
* number of elements considered in the fit is equal to the return
* value of \ref pcomp_poly_fit_num_of_samples.
*
* \returns \ref PCOMP_STAT_SUCCESS if the fit was computed
* successfully, \ref PCOMP_STAT_INVALID_FIT if the data are incorrect
* (e.g., there are fewer samples than unknowns).
*/
int pcomp_run_poly_fit(pcomp_poly_fit_data_t* poly_fit, double* coeffs,
const double* points)
{
size_t idx;
double chisq;
if (poly_fit == NULL || coeffs == NULL || points == NULL)
abort();
for (idx = 0; idx < poly_fit->num_of_samples; ++idx) {
gsl_vector_set(poly_fit->y, idx, points[idx]);
}
if (gsl_multifit_linear(poly_fit->matrix, poly_fit->y, poly_fit->c,
poly_fit->cov_matrix, &chisq,
poly_fit->workspace) != 0) {
return PCOMP_STAT_INVALID_FIT;
}
for (idx = 0; idx < poly_fit->num_of_coeffs; ++idx) {
coeffs[idx] = gsl_vector_get(poly_fit->c, idx);
}
return PCOMP_STAT_SUCCESS;
}
/***********************************************************************
* Types and functions used for computing Chebyshev transforms
*/
struct __pcomp_chebyshev_t {
double* input;
double* output;
size_t num_of_samples;
fftw_plan fftw_plan_ptr;
pcomp_transform_direction_t dir;
};
/** \ingroup cheby
*
* \brief Allocate a new instance of the \ref pcomp_chebyshev_t
* structure on the heap
*
* Despite the fact that this function takes the parameter \a dir, the
* function which actually computes the Chebyshev transform (\ref
* pcomp_run_chebyshev) allow to specify the desired direction. The
* purpose of having \a dir encoded in \ref pcomp_chebyshev_t is that
* sometimes it is useful to keep it memorized in the structure
* itself.
*
* \param[in] num_of_samples Number of floating-point numbers that
* will be transformed
*
* \param[in] dir Direction of the transform (either forward or
* backward). This is used to determine the normalization constant of
* the transform:
* - If computing a forward transform, the normalization is \f$1 / (N
* - 1)\f$, with \f$N\f$ the number of samples.
* - If computing a backward transform, the normalization is 1.
*
* \returns A newly created instance of \ref pcomp_poly_fit_data_t
* structure. This must be freed using \ref pcomp_free_poly_fit, once
* it is no longer used.
*/
pcomp_chebyshev_t* pcomp_init_chebyshev(size_t num_of_samples,
pcomp_transform_direction_t dir)
{
pcomp_chebyshev_t* chebyshev = malloc(sizeof(pcomp_chebyshev_t));
if (chebyshev == NULL)
abort();
chebyshev->input = fftw_alloc_real(num_of_samples);
chebyshev->output = fftw_alloc_real(num_of_samples);
chebyshev->num_of_samples = num_of_samples;
chebyshev->fftw_plan_ptr = fftw_plan_r2r_1d(
num_of_samples, chebyshev->input, chebyshev->output,
FFTW_REDFT00, FFTW_ESTIMATE);
chebyshev->dir = dir;
return chebyshev;
}
/** \ingroup cheby
*
* \brief Free the memory allocated by a previous call to \ref
* pcomp_init_chebyshev.
*
* \param[in] plan Pointer to the structure to be freed.
*/
void pcomp_free_chebyshev(pcomp_chebyshev_t* plan)
{
if (plan == NULL)
return;
if (plan->input != NULL)
fftw_free(plan->input);
if (plan->output != NULL)
fftw_free(plan->output);
if (plan->fftw_plan_ptr != NULL)
fftw_destroy_plan(plan->fftw_plan_ptr);
free(plan);
}
/** \ingroup cheby
*
* \brief Return the number of samples in a Chebyshev transform
*
* \param[in] plan Pointer to the Chebyshev plan.
*
* \returns The number of elements that are used in the Chebyshev
* transform specified by \a plan.
*/
size_t pcomp_chebyshev_num_of_samples(const pcomp_chebyshev_t* plan)
{
if (plan == NULL)
abort();
return plan->num_of_samples;
}
/** \ingroup cheby
*
* \brief Return the direction of a Chebyshev transform
*
* \param[in] plan Pointer to the Chebyshev plan.
*
* \returns A \ref pcomp_transform_direction_t value specifying the
* normalization used for the Chebyshev transform specified by \a
* plan.
*/
pcomp_transform_direction_t
pcomp_chebyshev_direction(const pcomp_chebyshev_t* plan)
{
if (plan == NULL)
abort();
return plan->dir;
}
static double chebyshev_normalization(pcomp_transform_direction_t dir,
size_t num_of_samples)
{
if (dir == PCOMP_TD_DIRECT)
return 1.0 / (((double)num_of_samples) - 1.0);
else
return 0.5;
}
/** \ingroup cheby
*
* \brief Compute a forward/backward Chebyshev discrete transform
*
* \code{.c}
* #define NUM_OF_POINTS 3
* double points[NUM_OF_POINTS] = { 0.0, 1.0, 3.0 };
* double transform[NUM_OF_POINTS];
* pcomp_chebyshev_t* chebyshev;
* size_t idx;
*
* chebyshev = pcomp_init_chebyshev(NUM_OF_POINTS, PCOMP_TD_DIRECT);
* pcomp_run_chebyshev(chebyshev, PCOMP_TD_DIRECT, transform, points);
*
* puts("Transform:");
* for (idx = 0; idx < NUM_OF_POINTS; ++idx) {
* printf("%f\t", transform[idx]);
* }
* puts("");
* \endcode
*
* \param[in] plan Pointer to a Chebyshev plan created by \ref
* pcomp_init_chebyshev
*
* \param[in] dir Direction of the transform. This parameter overrides
* the internal direction of \a plan (returned by \ref
* pcomp_chebyshev_direction).
*
* \param[out] output Pointer to an array of \c double values that will
* contain the Chebyshev transform of \a input. It must have room for
* a number of elements at least equal to the return value of \ref
* pcomp_num_of_samples.
*
* \param[in] input Array of \c double values to be transformed. The
* function will use the first N elements, where N is the return value
* of \ref pcomp_num_of_samples.
*
* \returns \ref PCOMP_STAT_SUCCESS when successful.
*/
int pcomp_run_chebyshev(pcomp_chebyshev_t* plan,
pcomp_transform_direction_t dir, double* output,
const double* input)
{
double norm;
size_t idx;
if (plan == NULL)
abort();
if (input != NULL) {
for (idx = 0; idx < plan->num_of_samples; ++idx) {
plan->input[idx] = input[idx];
}
}
fftw_execute(plan->fftw_plan_ptr);
norm = chebyshev_normalization(dir, plan->num_of_samples);
for (idx = 0; idx < plan->num_of_samples; ++idx) {
plan->output[idx] *= norm;
}
if (output != NULL && output != plan->output) {
for (idx = 0; idx < plan->num_of_samples; ++idx) {
output[idx] = plan->output[idx];
}
}
return PCOMP_STAT_SUCCESS;
}
/** \ingroup cheby
*
* \brief Return the input data used in the last call to \ref
*pcomp_run_chebyshev
*
* If \ref pcomp_run_chebyshev was never called, the array returned by
* this function contains garbage.
*
* \param[in] plan Pointer to a Chebyshev plan created by \ref
* pcomp_init_chebyshev
*
* \return A pointer to the first element of the array of elements
* used as input by the last call to \ref pcomp_run_chebyshev.
*/
const double* pcomp_chebyshev_input(const pcomp_chebyshev_t* plan)
{
if (plan == NULL)
abort();
return plan->input;
}
/** \ingroup cheby
*
* \brief Return the output (Chebyshev transform) of the last call to
*\ref pcomp_run_chebyshev
*
* If \ref pcomp_run_chebyshev was never called, the array returned by
* this function contains garbage.
*
* \param[in] plan Pointer to a Chebyshev plan created by \ref
* pcomp_init_chebyshev
*
* \return A pointer to the first element of the array of elements
* containing the output of the last call to \ref pcomp_run_chebyshev.
*/
const double* pcomp_chebyshev_output(const pcomp_chebyshev_t* plan)
{
if (plan == NULL)
abort();
return plan->output;
}
/***********************************************************************
* Types and functions used for applying the combined
* fitting/Chebyshev transforms
*/
struct __pcomp_polycomp_t {
size_t samples_per_chunk;
pcomp_poly_fit_data_t* poly_fit;
pcomp_chebyshev_t* chebyshev;
pcomp_chebyshev_t* inv_chebyshev;
double max_allowable_error;
pcomp_polycomp_algorithm_t algorithm;
double period;
};
/** \ingroup poly
*
* \brief Allocate space for a \ref pcomp_polycomp_t structure
*
* \param[in] samples_per_chunk Number of samples in each chunk
*
* \param[in] num_of_coeffs Number of polynomial coefficients to use
*
* \param[in] max_allowable_error Upper bound for the compression
* error (positive value)
*
* \param[in] algorithm Kind of compression algorithm to use
*
* \returns A pointer to the newly allocate \ref pcomp_polycomp_t
* structure. This must be freed using \ref pcomp_free_polycomp, once
* it is no longer used.
*/
pcomp_polycomp_t*
pcomp_init_polycomp(pcomp_chunk_size_t samples_per_chunk,
pcomp_poly_size_t num_of_coeffs,
double max_allowable_error,
pcomp_polycomp_algorithm_t algorithm)
{
pcomp_polycomp_t* params = malloc(sizeof(pcomp_polycomp_t));
if (params == NULL)
abort();
params->samples_per_chunk = samples_per_chunk;
params->poly_fit
= pcomp_init_poly_fit(samples_per_chunk, num_of_coeffs);
params->chebyshev
= pcomp_init_chebyshev(samples_per_chunk, PCOMP_TD_DIRECT);
params->inv_chebyshev
= pcomp_init_chebyshev(samples_per_chunk, PCOMP_TD_INVERSE);
params->max_allowable_error = max_allowable_error;
params->algorithm = algorithm;
params->period = 0.0;
return params;
}
/** \ingroup poly
*
* \brief Free the memory allocated by \ref pcomp_init_polycomp for a
* \ref pcomp_polycomp_t structure.
*
* \param[in] params Pointer to the structure to be freed
*/
void pcomp_free_polycomp(pcomp_polycomp_t* params)
{
if (params == NULL)
return;
pcomp_free_poly_fit(params->poly_fit);
pcomp_free_chebyshev(params->chebyshev);
pcomp_free_chebyshev(params->inv_chebyshev);
free(params);
}
/** \ingroup poly
*
* \brief Return the number of samples per chunk
*
* This function returns the size of each chunk but the last one in
* the input data for a polynomial compression. Such chunks contain a
* set of consecutive values in the input array passed to routines as
* \ref pcomp_compress_polycomp.
*
* \param[in] params Pointer to a \ref pcomp_polycomp_t structure
* containing the compression parameters
*
* \returns The number of samples in each chunk.
*/
pcomp_chunk_size_t
pcomp_polycomp_samples_per_chunk(const pcomp_polycomp_t* params)
{
if (params == NULL)
abort();
return params->samples_per_chunk;
}
/** \ingroup poly
*
* \brief Return the number of coefficients for the fitting polynomial
* used in the polynomial compression.
*
* The return value has the same meaning as the value returned by the
* \ref pcomp_poly_fit_num_of_coeffs.
*
* \param[in] params Pointer to a \ref pcomp_polycomp_t structure
* containing the compression parameters
*
* \returns The number of coefficients of the fitting polynomial.
*/
pcomp_poly_size_t
pcomp_polycomp_num_of_poly_coeffs(const pcomp_polycomp_t* params)
{
if (params == NULL || params->poly_fit == NULL)
abort();
return params->poly_fit->num_of_coeffs;
}
/** \ingroup poly
*
* \brief Return the upper bound on the error of the polynomial
*compression.
*
* \param[in] params Pointer to a \ref pcomp_polycomp_t structure
* containing the compression parameters
*
* \returns The maximum allowable error for the polynomial compression.
*/
double pcomp_polycomp_max_error(const pcomp_polycomp_t* params)
{
if (params == NULL)
abort();
return params->max_allowable_error;
}
/** \ingroup poly
*
* \brief Return the kind of algorithm used for a polynomial
*compression.
*
* \param[in] params Pointer to a \ref pcomp_polycomp_t structure
* containing the compression parameters
*
* \returns The algorithm to be used by the compressor.
*/
pcomp_polycomp_algorithm_t
pcomp_polycomp_algorithm(const pcomp_polycomp_t* params)
{
if (params == NULL)
abort();
return params->algorithm;
}
/** \ingroup poly_lowlevel
*
* \brief Return a pointer to a \ref pcomp_chebyshev_t structure
* representing the forward Chebyshev transform.
*
* \param[in] params Pointer to a \ref pcomp_polycomp_t structure
* containing the compression parameters
*
* \returns The algorithm to be used by the compressor.
*/
pcomp_chebyshev_t*
pcomp_polycomp_forward_cheby(const pcomp_polycomp_t* params)
{
if (params == NULL)
abort();
return params->chebyshev;
}
/** \ingroup poly_lowlevel
*
* \brief Return a pointer to a \ref pcomp_chebyshev_t structure
* representing the forward Chebyshev transform.
*
* \param[in] params Pointer to a \ref pcomp_polycomp_t structure
* containing the compression parameters
*
* \returns The algorithm to be used by the compressor.
*/
pcomp_chebyshev_t*
pcomp_polycomp_backward_cheby(const pcomp_polycomp_t* params)
{
if (params == NULL)
abort();
return params->inv_chebyshev;
}
/** \ingroup poly
*
* \brief Return the period of the input data, or a number
* less than or equal to 0 if the data have no periodicity.
*
* See also \ref pcomp_polycomp_set_period.
*
* \param[in] params Pointer to a \ref pcomp_polycomp_t structure
* containing the compression parameters
*
* \returns The periodicity. If zero or negative, no periodicity is
* assumed in the data to be compressed.
*/
double pcomp_polycomp_period(const pcomp_polycomp_t* params)
{
if (params == NULL)
abort();
return params->period;
}
/** \ingroup poly
*
* \brief Set the periodicity of the data to be compressed
*
* If \a period is a value greater than zero, this is assumed to be
* the periodicity of the input data: the value \a x is therefore
* assumed equivalent to \a x + \a period and to \a x - \a period. It
* is typically a multiple of Pi = 3.14159...
*
* The polynomial compressor can improve the compression ratio for
* data if they have some form of periodicity.
*
* \param[in] params Pointer to a \ref pcomp_polycomp_t structure
* containing the compression parameters
*
* \param[in] period The periodicity of the data, or a zero/negative
* value if no periodicity should be assumed by the compressor.
*/
void pcomp_polycomp_set_period(pcomp_polycomp_t* params, double period)
{
if (params == NULL)
abort();
params->period = period;
}
/***********************************************************************
* Evaluate the value of a polynomial at a point using Horner's formula
*/
static double eval_poly(double* coeffs, size_t num_of_coeffs, double x)
{
if (coeffs == NULL)
abort();
if (num_of_coeffs >= 1) {
int idx = num_of_coeffs - 1;
double result = coeffs[idx];
if (num_of_coeffs == 1)
return result;
for (idx = num_of_coeffs - 2; idx >= 0; --idx)
result = result * x + coeffs[idx];
return result;
}
else
return 0.0;
}
/***********************************************************************/
/** \ingroup poly_lowlevel
*
* \brief Remove sudden jumps from \a input
*
* Assuming that the data in the array \a input have a periodicity
* equal to \a period, the function copies them to \a output while
* applying a positive/negative offset equal to a multiple of \a
* period.
*
* It is ok for \a input and \a output to point to the same memory
* location.
*
* \param[out] output Pointer to the array that will contain the
* result. It must have room for at least \a num_of_samples values.
*
* \param[in] input Array of \a num_of_samples values to process.
*
* \param[in] num_of_samples Number of samples to process in \a input
*
* \param[in] period Periodicity of the data. If less or equal to
* zero, \a input is copied verbatim to \a output.
*/
void pcomp_straighten(double* output, const double* input,
size_t num_of_samples, double period)
{
size_t idx;
if (input == NULL || output == NULL)
abort();
if (period > 0) {
double half_period = period * 0.5;
double offset = 0.0;
output[0] = input[0];
for (idx = 1; idx < num_of_samples; ++idx) {
double diff_with_previous = input[idx] - input[idx - 1];
if (diff_with_previous > half_period)
offset -= period;
else if (diff_with_previous < -half_period)
offset += period;
output[idx] = input[idx] + offset;
}
}
else {
for (idx = 0; idx < num_of_samples; ++idx)
output[idx] = input[idx];
}
}
/***********************************************************************
* Chunk initialization/destruction
*/
/* Information about a chunk of data compressed using the polynomial
* compression */
struct __pcomp_polycomp_chunk_t {
/* Number of samples in this chunk */
size_t num_of_samples;
/* Is this chunk compressed using polynomial/Chebyshev
* coefficients? */
int is_compressed;
/* If the chunk is not compressed (is_compressed == 0), this
* points to a buffer which holds "num_of_samples" uncompressed
* samples */
double* uncompressed;
/* Polynomial coefficients, from the lowest-order to the
* highest-order */
size_t num_of_poly_coeffs;
double* poly_coeffs;
/* Chebyshev coefficients */
uint8_t* cheby_mask;
size_t num_of_cheby_coeffs; /* This is always less than
* num_of_samples, as the Chebyshev
* series is chopped. */
double* cheby_coeffs;
};
/** \ingroup poly_lowlevel
*
* \brief Allocate memory for a \ref pcomp_polycomp_chunk_t object
*
* \param[in] num_of_samples Number of samples that the chunk will be
* capable to hold.
*
* \return A pointer to the newly allocated object. Use \ref
* pcomp_free_chunk to free the memory once is no longer needed.
*/
pcomp_polycomp_chunk_t*
pcomp_init_chunk(pcomp_chunk_size_t num_of_samples)
{
pcomp_polycomp_chunk_t* chunk
= malloc(sizeof(pcomp_polycomp_chunk_t));
if (chunk == NULL)
abort();
chunk->num_of_samples = num_of_samples;
chunk->is_compressed = 0;
chunk->uncompressed
= malloc(sizeof(double) * sizeof(chunk->num_of_samples));
chunk->num_of_poly_coeffs = 0;
chunk->poly_coeffs = NULL;
chunk->num_of_cheby_coeffs = 0;
chunk->cheby_coeffs = NULL;
chunk->cheby_mask = NULL;
return chunk;
}
/** \ingroup poly_lowlevel
*
* \brief Allocate memory for a \ref pcomp_polycomp_chunk_t object and
* fill it with data in uncompressed form.
*
* \param[in] num_of_samples Number of samples that the chunk will be
* capable to hold.
*
* \param[in] samples The (uncompressed) samples to copy into the
* chunk. After the call, \a input is no longer needed and can be
* freed without invalidating the pointer returned by the function.
*
* \return A pointer to the newly allocated object. Use \ref
* pcomp_free_chunk to free the memory once is no longer needed.
*/
pcomp_polycomp_chunk_t*
pcomp_init_uncompressed_chunk(pcomp_chunk_size_t num_of_samples,
const double* samples)
{
pcomp_polycomp_chunk_t* chunk
= malloc(sizeof(pcomp_polycomp_chunk_t));
const size_t num_of_bytes = sizeof(double) * num_of_samples;
if (chunk == NULL)
abort();
chunk->num_of_samples = num_of_samples;
chunk->is_compressed = 0;
chunk->uncompressed = malloc(num_of_bytes);
if (chunk->uncompressed == NULL)
abort();
memcpy(chunk->uncompressed, samples, num_of_bytes);
chunk->num_of_poly_coeffs = 0;
chunk->poly_coeffs = NULL;
chunk->num_of_cheby_coeffs = 0;
chunk->cheby_coeffs = NULL;