blob: 62087d69fb72736db30eaf40db0a43135784a21d [file] [log] [blame]
Piotr Krysik47ab20e2016-07-04 21:23:51 +02001/* -*- c++ -*- */
2/* @file
3 * @author Piotr Krysik <ptrkrysik@gmail.com>
4 * @section LICENSE
5 *
6 * Gr-gsm is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3, or (at your option)
9 * any later version.
10 *
11 * Gr-gsm is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with gr-gsm; see the file COPYING. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street,
19 * Boston, MA 02110-1301, USA.
20 *
21 */
22
23#ifdef HAVE_CONFIG_H
24#include "config.h"
25#endif
26
27#include <gnuradio/io_signature.h>
28#include "controlled_fractional_resampler_cc_impl.h"
29#include <stdexcept>
30
31namespace gr {
32 namespace gsm {
33
34 controlled_fractional_resampler_cc::sptr
35 controlled_fractional_resampler_cc::make(float phase_shift, float resamp_ratio)
36 {
37 return gnuradio::get_initial_sptr
38 (new controlled_fractional_resampler_cc_impl(phase_shift, resamp_ratio));
39 }
40
41 controlled_fractional_resampler_cc_impl::controlled_fractional_resampler_cc_impl
42 (float phase_shift, float resamp_ratio)
43 : block("controlled_fractional_resampler_cc",
44 io_signature::make(1, 1, sizeof(gr_complex)),
45 io_signature::make(1, 1, sizeof(gr_complex))),
46 d_mu(phase_shift), d_mu_inc(resamp_ratio),
47 d_resamp(new mmse_fir_interpolator_cc())
48 {
49 this->set_tag_propagation_policy(TPP_DONT);
50 if(resamp_ratio <= 0)
51 throw std::out_of_range("resampling ratio must be > 0");
52 if(phase_shift < 0 || phase_shift > 1)
53 throw std::out_of_range("phase shift ratio must be > 0 and < 1");
54
55 set_relative_rate(1.0 / resamp_ratio);
56 }
57
58 controlled_fractional_resampler_cc_impl::~controlled_fractional_resampler_cc_impl()
59 {
60 delete d_resamp;
61 }
62
63 void
64 controlled_fractional_resampler_cc_impl::forecast(int noutput_items,
65 gr_vector_int &ninput_items_required)
66 {
67 unsigned ninputs = ninput_items_required.size();
68 for(unsigned i=0; i < ninputs; i++) {
69 ninput_items_required[i] =
70 (int)ceil((noutput_items * d_mu_inc) + d_resamp->ntaps());
71 }
72 }
73
74 int
75 controlled_fractional_resampler_cc_impl::general_work(int noutput_items,
76 gr_vector_int &ninput_items,
77 gr_vector_const_void_star &input_items,
78 gr_vector_void_star &output_items)
79 {
80 const gr_complex *in = (const gr_complex*)input_items[0];
81 gr_complex *out = (gr_complex*)output_items[0];
82
83 uint64_t processed_in = 0; //input samples processed in the last call to resample function
84 uint64_t processed_in_sum = 0; //input samples processed during a whole call to general_work function
85 uint64_t produced_out_sum = 0; //output samples produced during a whole call to general_work function
86
87 std::vector<tag_t> set_resamp_ratio_tags;
88
89 pmt::pmt_t key = pmt::string_to_symbol("set_resamp_ratio");
Piotr Krysik74c4f2c2016-07-15 13:12:46 +020090 get_tags_in_window(set_resamp_ratio_tags, 0, 0, ninput_items[0]);
Piotr Krysik47ab20e2016-07-04 21:23:51 +020091
Piotr Krysik74c4f2c2016-07-15 13:12:46 +020092// std::cout << "-----------------------------" << std::endl;
93// std::cout << "ninput_items[0] " << ninput_items[0] << std::endl;
94// std::cout << "noutput_items " << noutput_items << std::endl;
Piotr Krysik47ab20e2016-07-04 21:23:51 +020095
Piotr Krysik74c4f2c2016-07-15 13:12:46 +020096 bool all_output_samples_produced = false;
Piotr Krysik47ab20e2016-07-04 21:23:51 +020097 for(std::vector<tag_t>::iterator i_tag = set_resamp_ratio_tags.begin(); i_tag < set_resamp_ratio_tags.end(); i_tag++)
98 {
Piotr Krysik74c4f2c2016-07-15 13:12:46 +020099 uint64_t tag_offset_rel = i_tag->offset - nitems_read(0);
100
101 if(pmt::symbol_to_string(i_tag->key) == "set_resamp_ratio")
102 {
103 uint64_t samples_to_produce = static_cast<uint64_t>(round(static_cast<double>(tag_offset_rel-processed_in_sum)/d_mu_inc)); //tu może być problem - bo to jest głupota przy d_mu_inc różnym od 1.0
104
105 if( (samples_to_produce + produced_out_sum) > noutput_items)
106 {
107 samples_to_produce = noutput_items - produced_out_sum;
108 all_output_samples_produced = true;
109 }
110
111 // std::cout << "samples_to_produce = (tag_offset_rel-processed_in_sum)/d_mu_inc: " << samples_to_produce << " = " << tag_offset_rel << " - " << processed_in_sum << std::endl;
112 processed_in = resample(in, processed_in_sum, out, produced_out_sum, samples_to_produce);
113 processed_in_sum = processed_in_sum + processed_in;
114 produced_out_sum = produced_out_sum + samples_to_produce;
115
116 // std::cout << "d_ii " << d_ii << std::endl;
117 // std::cout << "produced_out_sum + nitems_written(0) " << produced_out_sum + nitems_written(0) << std::endl;
118 if(all_output_samples_produced)
119 {
120 break;
121 } else {
122 add_item_tag(0, produced_out_sum + nitems_written(0), i_tag->key, i_tag->value);
123 set_resamp_ratio(pmt::to_double(i_tag->value));
124 }
125 } else {
126 uint64_t out_samples_to_tag = round(static_cast<double>(tag_offset_rel-processed_in_sum)/d_mu_inc);
127 if( (out_samples_to_tag + produced_out_sum) <= noutput_items)
128 {
129 add_item_tag(0, produced_out_sum + out_samples_to_tag + nitems_written(0), i_tag->key, i_tag->value);
130 }
131 }
132// std::cout << "processed_in: " << processed_in << " tag_offset_rel: " << tag_offset_rel << " produced_out_sum: " << produced_out_sum << std::endl;
Piotr Krysik47ab20e2016-07-04 21:23:51 +0200133// std::cout << "Setting resamp ratio: " << d_mu_inc << std::endl;
134 }
135
Piotr Krysik74c4f2c2016-07-15 13:12:46 +0200136// std::cout << "noutput_items: " << noutput_items << " produced_out_sum: " << produced_out_sum << std::endl;
137// std::cout << "last_resample_outputs: " << (noutput_items-produced_out_sum) << std::endl;
Piotr Krysik47ab20e2016-07-04 21:23:51 +0200138// processed_in = resample(in, processed_in_sum, out, produced_out_sum, samples_to_produce);
139// processed_in_sum = processed_in_sum + processed_in;
140// produced_out_sum = produced_out_sum + samples_to_produce;
Piotr Krysik47ab20e2016-07-04 21:23:51 +0200141// processed_in = resample(in, 0, out, 0, noutput_items);
Piotr Krysik74c4f2c2016-07-15 13:12:46 +0200142
143 if(!all_output_samples_produced)
144 {
145 processed_in = resample(in, processed_in_sum, out, produced_out_sum, (noutput_items-produced_out_sum));
146 processed_in_sum = processed_in_sum + processed_in;
147 }
148
Piotr Krysik47ab20e2016-07-04 21:23:51 +0200149 consume_each(processed_in_sum);
150 return noutput_items;
151 }
152
153 inline uint64_t
154 controlled_fractional_resampler_cc_impl::resample(const gr_complex *in, uint64_t first_in_sample, gr_complex *out, uint64_t first_out_sample, uint64_t samples_to_produce)
155 {
156 int ii = first_in_sample;
157 int oo = first_out_sample;
158 while(oo < (first_out_sample+samples_to_produce)) //produce samples_to_produce number of samples
159 {
160 out[oo++] = d_resamp->interpolate(&in[ii], d_mu);
161
162 double s = d_mu + d_mu_inc;
163 double f = floor(s);
164 int incr = (int)f;
165 d_mu = s - f;
166 ii += incr;
167 }
168 return ii-first_in_sample; //number of input samples processed
169 }
170
171 float
172 controlled_fractional_resampler_cc_impl::mu() const
173 {
174 return d_mu;
175 }
176
177 float
178 controlled_fractional_resampler_cc_impl::resamp_ratio() const
179 {
180 return d_mu_inc;
181 }
182
183 void
184 controlled_fractional_resampler_cc_impl::set_mu(float mu)
185 {
186 d_mu = mu;
187 }
188
189 void
190 controlled_fractional_resampler_cc_impl::set_resamp_ratio(float resamp_ratio)
191 {
192 d_mu_inc = resamp_ratio;
193 set_relative_rate(1.0 / resamp_ratio);
194 }
195
196 } /* namespace grgsm */
197} /* namespace gr */
198