Imported Upstream version 3.2.2
[debian/gnuradio] / gnuradio-core / src / lib / general / gr_squash_ff.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2008 Free Software Foundation, Inc.
4  * 
5  * This file is part of GNU Radio
6  * 
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3, or (at your option)
10  * any later version.
11  * 
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with GNU Radio; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <stdexcept>
28 #include <gr_squash_ff.h>
29 #include <gr_io_signature.h>
30
31 // expect input vector of igrid.size y-values,
32 // produce output vector of ogrid.size y-values
33
34 gr_squash_ff_sptr
35 gr_make_squash_ff(const std::vector<float> &igrid,
36                   const std::vector<float> &ogrid)
37 {
38   return gr_squash_ff_sptr(new gr_squash_ff(igrid, ogrid));
39 }
40
41 gr_squash_ff::gr_squash_ff(const std::vector<float> &igrid,
42                            const std::vector<float> &ogrid)
43   : gr_sync_block("squash_ff",
44                   gr_make_io_signature(1, 1, sizeof(float) * igrid.size()),
45                   gr_make_io_signature(1, 1, sizeof(float) * ogrid.size()))
46 {
47   d_inum  = igrid.size();
48   d_onum  = ogrid.size();
49   d_igrid = (double *) malloc(d_inum * sizeof(double));
50   d_iwork = (double *) malloc(d_inum * sizeof(double));
51   d_ogrid = (double *) malloc(d_onum * sizeof(double));
52   for (unsigned int i = 0; i < d_inum; i++)
53     d_igrid[i] = igrid[i];
54   for (unsigned int i = 0; i < d_onum; i++)
55     d_ogrid[i] = ogrid[i];
56
57   d_accel = gsl_interp_accel_alloc();
58   d_spline = gsl_spline_alloc(gsl_interp_cspline, d_inum);      // FIXME check w/ Frank
59 }
60
61 gr_squash_ff::~gr_squash_ff()
62 {
63   free((char *) d_igrid);
64   free((char *) d_iwork);
65   free((char *) d_ogrid);
66   gsl_interp_accel_free(d_accel);
67   gsl_spline_free(d_spline);
68 }
69
70 int
71 gr_squash_ff::work(int noutput_items,
72                     gr_vector_const_void_star &input_items,
73                     gr_vector_void_star &output_items)
74 {
75   const float *in  = (const float *) input_items[0];
76   float       *out = (float *) output_items[0];
77
78   for (int count = 0; count < noutput_items; count++) {
79
80     for (unsigned int i = 0; i < d_inum; i++)
81       d_iwork[i] = in[i];
82
83     gsl_spline_init(d_spline, d_igrid, d_iwork, d_inum);
84     
85     for (unsigned int i = 0; i < d_onum; i++)
86       out[i] = gsl_spline_eval(d_spline, d_ogrid[i], d_accel);
87
88     in  += d_inum;
89     out += d_onum;
90   }
91
92   return noutput_items;
93 }