|
| 1 | +/* |
| 2 | +This software is part of libcsdr, a set of simple DSP routines for |
| 3 | +Software Defined Radio. |
| 4 | +
|
| 5 | +Copyright (c) 2014, Andras Retzler <[email protected]> |
| 6 | +All rights reserved. |
| 7 | +
|
| 8 | +Redistribution and use in source and binary forms, with or without |
| 9 | +modification, are permitted provided that the following conditions are met: |
| 10 | + * Redistributions of source code must retain the above copyright |
| 11 | + notice, this list of conditions and the following disclaimer. |
| 12 | + * Redistributions in binary form must reproduce the above copyright |
| 13 | + notice, this list of conditions and the following disclaimer in the |
| 14 | + documentation and/or other materials provided with the distribution. |
| 15 | + * Neither the name of the copyright holder nor the |
| 16 | + names of its contributors may be used to endorse or promote products |
| 17 | + derived from this software without specific prior written permission. |
| 18 | +
|
| 19 | +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 20 | +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 21 | +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 22 | +DISCLAIMED. IN NO EVENT SHALL ANDRAS RETZLER BE LIABLE FOR ANY |
| 23 | +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 24 | +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 25 | +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 26 | +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 28 | +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | +*/ |
| 30 | + |
| 31 | +#include "fastddc.h" |
| 32 | + |
| 33 | +//DDC implementation based on: |
| 34 | +//http://www.3db-labs.com/01598092_MultibandFilterbank.pdf |
| 35 | + |
| 36 | +inline int is_integer(float a) { return floorf(a) == a; } |
| 37 | + |
| 38 | +int fastddc_init(fastddc_t* ddc, float transition_bw, int decimation, float shift_rate) |
| 39 | +{ |
| 40 | + ddc->pre_decimation = 1; //this will be done in the frequency domain |
| 41 | + ddc->post_decimation = decimation; //this will be done in the time domain |
| 42 | + while( is_integer((float)ddc->post_decimation/2) && ddc->post_decimation/2 != 1) |
| 43 | + { |
| 44 | + ddc->post_decimation/=2; |
| 45 | + ddc->pre_decimation*=2; |
| 46 | + } |
| 47 | + ddc->taps_real_length = firdes_filter_len(transition_bw); //the number of non-zero taps |
| 48 | + ddc->taps_length = ceil(ddc->taps_real_length/(float)ddc->pre_decimation) * ddc->pre_decimation; //the number of taps must be a multiple of the decimation factor |
| 49 | + ddc->fft_size = next_pow2(ddc->taps_length * 4); //it is a good rule of thumb for performance (based on the article), but we should do benchmarks |
| 50 | + while (ddc->fft_size<ddc->pre_decimation) ddc->fft_size*=2; //fft_size should be a multiple of pre_decimation |
| 51 | + ddc->overlap_length = ddc->taps_length - 1; |
| 52 | + ddc->input_size = ddc->fft_size - ddc->overlap_length; |
| 53 | + ddc->fft_inv_size = ddc->fft_size / ddc->pre_decimation; |
| 54 | + |
| 55 | + //Shift operation in the frequency domain: we can shift by a multiple of v. |
| 56 | + ddc->v = ddc->fft_size/ddc->overlap_length; //+-1 ? (or maybe ceil() this?) //TODO: why? |
| 57 | + int middlebin=ddc->fft_size / 2; |
| 58 | + ddc->startbin = middlebin + middlebin * shift_rate * 2; |
| 59 | + ddc->startbin = ddc->v * round( ddc->startbin / (float)ddc->v ); |
| 60 | + ddc->offsetbin = ddc->startbin - middlebin; |
| 61 | + ddc->post_shift = ((float)ddc->offsetbin/ddc->fft_size) - shift_rate; |
| 62 | + ddc->pre_shift = ddc->offsetbin * ddc->v; |
| 63 | + |
| 64 | + //Overlap is scraped, not added |
| 65 | + ddc->scrape=ddc->overlap_length/ddc->pre_decimation; |
| 66 | + ddc->output_size=ddc->fft_inv_size-ddc->scrape; |
| 67 | + |
| 68 | + return ddc->fft_size<=2; //returns true on error |
| 69 | +} |
| 70 | + |
| 71 | + |
| 72 | +void fastddc_print(fastddc_t* ddc) |
| 73 | +{ |
| 74 | + fprintf(stderr, |
| 75 | + "fastddc_print_sizes(): (fft_size = %d) = (taps_length = %d) + (input_size = %d) - 1\n" |
| 76 | + "\t(overlap_length = %d) = taps_length - 1, taps_real_length = %d\n" |
| 77 | + "\tdecimation = (pre_decimation = %d) * (post_decimation = %d), fft_inv_size = %d\n" |
| 78 | + "\tstartbin = %d, offsetbin = %d, v = %d, pre_shift = %g, post_shift = %g\n" |
| 79 | + , |
| 80 | + ddc->fft_size, ddc->taps_length, ddc->input_size, |
| 81 | + ddc->overlap_length, ddc->taps_real_length, |
| 82 | + ddc->pre_decimation, ddc->post_decimation, ddc->fft_inv_size, |
| 83 | + ddc->startbin, ddc->offsetbin, ddc->v, ddc->pre_shift, ddc->post_shift ); |
| 84 | +} |
| 85 | + |
| 86 | +decimating_shift_addition_status_t fastddc_apply_cc(complexf* input, complexf* output, fastddc_t* ddc, FFT_PLAN_T* plan_inverse, complexf* taps_fft, decimating_shift_addition_status_t shift_stat) |
| 87 | +{ |
| 88 | + //implements DDC by using the overlap & scrape method |
| 89 | + //TODO: +/-1s on overlap_size et al |
| 90 | + //input shoud have ddc->fft_size number of elements |
| 91 | + |
| 92 | + complexf* inv_input = plan_inverse->input; |
| 93 | + complexf* inv_output = plan_inverse->output; |
| 94 | + |
| 95 | + //Initialize buffers for inverse FFT to zero |
| 96 | + for(int i=0;i<plan_inverse->size;i++) |
| 97 | + { |
| 98 | + iof(inv_input,i)=0; |
| 99 | + qof(inv_input,i)=0; |
| 100 | + } |
| 101 | + |
| 102 | + //Alias & shift & filter at once |
| 103 | + // * no, we won't break this algorithm to parts that are easier to understand: now we go for speed |
| 104 | + for(int i=0;i<ddc->fft_size;i++) |
| 105 | + { |
| 106 | + int output_index = (ddc->startbin+i)%plan_inverse->size; |
| 107 | + int tap_index = (ddc->fft_size+i-ddc->offsetbin)%ddc->fft_size; |
| 108 | + cmultadd(inv_input+output_index, input+i, taps_fft+tap_index); //cmultadd(output, input1, input2): complex output += complex input1 * complex input 2 |
| 109 | + } |
| 110 | + |
| 111 | + fft_execute(plan_inverse); |
| 112 | + |
| 113 | + //Normalize data |
| 114 | + for(int i=0;i<plan_inverse->size;i++) //@apply_ddc_fft_cc: normalize by size |
| 115 | + { |
| 116 | + iof(inv_output,i)/=plan_inverse->size; |
| 117 | + qof(inv_output,i)/=plan_inverse->size; |
| 118 | + } |
| 119 | + |
| 120 | + //Overlap is scraped, not added |
| 121 | + //Shift correction |
| 122 | + shift_addition_data_t dsadata=decimating_shift_addition_init(ddc->post_shift, ddc->post_decimation); //this could be optimized (passed as parameter), but we would not win too much at all |
| 123 | + shift_stat=decimating_shift_addition_cc(plan_inverse->output+ddc->scrape, output, ddc->output_size, dsadata, ddc->post_decimation, shift_stat); |
| 124 | + return shift_stat; |
| 125 | +} |
0 commit comments