Piotr Krysik | 47ab20e | 2016-07-04 21:23:51 +0200 | [diff] [blame] | 1 | /* -*- 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 | |
| 31 | namespace 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"); |
| 90 | get_tags_in_window(set_resamp_ratio_tags, 0, 0, noutput_items, key); |
| 91 | |
| 92 | std::cout << "-----------------------------" << std::endl; |
| 93 | |
| 94 | for(std::vector<tag_t>::iterator i_tag = set_resamp_ratio_tags.begin(); i_tag < set_resamp_ratio_tags.end(); i_tag++) |
| 95 | { |
| 96 | uint64_t tag_offset_rel = i_tag->offset-nitems_read(0); |
| 97 | uint64_t samples_to_produce = (tag_offset_rel-processed_in_sum)/d_mu_inc; //tu może być problem |
| 98 | 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; |
| 99 | processed_in = resample(in, processed_in_sum, out, produced_out_sum, samples_to_produce); |
| 100 | processed_in_sum = processed_in_sum + processed_in; |
| 101 | produced_out_sum = produced_out_sum + samples_to_produce; |
| 102 | std::cout << "processed_in: " << processed_in << " tag_offset_rel: " << tag_offset_rel << " produced_out_sum: " << produced_out_sum << std::endl; |
| 103 | set_resamp_ratio(pmt::to_double(i_tag->value)); |
| 104 | // std::cout << "Setting resamp ratio: " << d_mu_inc << std::endl; |
| 105 | } |
| 106 | |
| 107 | std::cout << "noutput_items: " << noutput_items << " produced_out_sum: " << produced_out_sum << std::endl; |
| 108 | std::cout << "last_resample_outputs: " << (noutput_items-produced_out_sum) << std::endl; |
| 109 | // processed_in = resample(in, processed_in_sum, out, produced_out_sum, samples_to_produce); |
| 110 | // processed_in_sum = processed_in_sum + processed_in; |
| 111 | // produced_out_sum = produced_out_sum + samples_to_produce; |
| 112 | |
| 113 | processed_in = resample(in, processed_in_sum, out, produced_out_sum, (noutput_items-produced_out_sum)); |
| 114 | // processed_in = resample(in, 0, out, 0, noutput_items); |
| 115 | processed_in_sum = processed_in_sum + processed_in; |
| 116 | consume_each(processed_in_sum); |
| 117 | return noutput_items; |
| 118 | } |
| 119 | |
| 120 | inline uint64_t |
| 121 | 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) |
| 122 | { |
| 123 | int ii = first_in_sample; |
| 124 | int oo = first_out_sample; |
| 125 | while(oo < (first_out_sample+samples_to_produce)) //produce samples_to_produce number of samples |
| 126 | { |
| 127 | out[oo++] = d_resamp->interpolate(&in[ii], d_mu); |
| 128 | |
| 129 | double s = d_mu + d_mu_inc; |
| 130 | double f = floor(s); |
| 131 | int incr = (int)f; |
| 132 | d_mu = s - f; |
| 133 | ii += incr; |
| 134 | } |
| 135 | return ii-first_in_sample; //number of input samples processed |
| 136 | } |
| 137 | |
| 138 | float |
| 139 | controlled_fractional_resampler_cc_impl::mu() const |
| 140 | { |
| 141 | return d_mu; |
| 142 | } |
| 143 | |
| 144 | float |
| 145 | controlled_fractional_resampler_cc_impl::resamp_ratio() const |
| 146 | { |
| 147 | return d_mu_inc; |
| 148 | } |
| 149 | |
| 150 | void |
| 151 | controlled_fractional_resampler_cc_impl::set_mu(float mu) |
| 152 | { |
| 153 | d_mu = mu; |
| 154 | } |
| 155 | |
| 156 | void |
| 157 | controlled_fractional_resampler_cc_impl::set_resamp_ratio(float resamp_ratio) |
| 158 | { |
| 159 | d_mu_inc = resamp_ratio; |
| 160 | set_relative_rate(1.0 / resamp_ratio); |
| 161 | } |
| 162 | |
| 163 | } /* namespace grgsm */ |
| 164 | } /* namespace gr */ |
| 165 | |