Advertisement
carruinar

Fractional delay

Nov 19th, 2014
380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.06 KB | None | 0 0
  1. /* -*- c++ -*- */
  2. /*
  3.  * Copyright 2014 Carlos Alberto Ruiz Naranjo,
  4.  * Center for Advanced Aerospace Technologies (CATEC).
  5.  *
  6.  * This 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.  * This software 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 this software; 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. #ifdef HAVE_CONFIG_H
  23. #include "config.h"
  24. #endif
  25.  
  26. #include <gnuradio/io_signature.h>
  27. #include "fractional_delay_impl.h"
  28. #include <math.h>
  29. #include <gnuradio/blocks/complex_to_float.h>
  30.  
  31. namespace gr {
  32.   namespace howto {
  33.  
  34.     fractional_delay::sptr
  35.     fractional_delay::make(float delay)
  36.     {
  37.       return gnuradio::get_initial_sptr
  38.         (new fractional_delay_impl(delay));
  39.     }
  40.  
  41.     /*
  42.      * The private constructor
  43.      */
  44.     fractional_delay_impl::fractional_delay_impl(float delay)
  45.       : gr::block("fractional_delay",
  46.               gr::io_signature::make(2,2, sizeof(float)),
  47.               gr::io_signature::make(2,2, sizeof(float)))
  48.     {
  49.         this->delay=delay;
  50.         k=0;
  51.     }
  52.  
  53.     /*
  54.      * Our virtual destructor.
  55.      */
  56.     fractional_delay_impl::~fractional_delay_impl()
  57.     {
  58.     }
  59.  
  60.     void
  61.     fractional_delay_impl::forecast (int noutput_items, gr_vector_int &ninput_items_required)
  62.     {
  63.         ninput_items_required[0] = noutput_items;
  64.         ninput_items_required[1] = noutput_items;
  65.     }
  66.  
  67.  
  68.  
  69.     int
  70.     fractional_delay_impl::general_work (int noutput_items,
  71.                        gr_vector_int &ninput_items,
  72.                        gr_vector_const_void_star &input_items,
  73.                        gr_vector_void_star &output_items)
  74.     {
  75.         int fft_length = 1024*10;
  76.  
  77.         const float *in_real = (const float *) input_items[0];
  78.         const float *in_img = (const float *) input_items[1];
  79.  
  80.         float *out_real = (float *) output_items[0];
  81.         float *out_img = (float *) output_items[1];
  82.  
  83.         for(int i=0;i<noutput_items;++i){
  84.             //float argumento = ( (2*M_PI/fft_length)*delay*k );
  85.  
  86.             //out_real[i] = in_real[i]*cos(argumento) + in_img[i]*sin(argumento);
  87.             //out_img[i] = -in_real[i]*sin(argumento) + in_img[i]*cos(argumento);
  88.  
  89.             if(k==0){
  90.                 out_real[i] = in_real[i];
  91.                 out_img[i] = in_img[i];
  92.             }
  93.             else if( (k>0) && (k<(fft_length/2)) ){
  94.                 float argumento = ( (2*M_PI/fft_length)*delay*k );
  95.  
  96.                 out_real[i] = in_real[i]*cos(argumento) + in_img[i]*sin(argumento);
  97.                 out_img[i] = -in_real[i]*sin(argumento) + in_img[i]*cos(argumento);
  98.             }
  99.             else if(k==fft_length/2){
  100.                 out_real[i] = in_real[i]*cos(M_PI*delay);
  101.                 out_img[i] = in_img[i]*cos(M_PI*delay);
  102.             }
  103.             else if(k>(fft_length/2) ){
  104.                 float argumento = ( (2*M_PI/fft_length)*delay*(fft_length-k) );
  105.  
  106.                 out_real[i] = in_real[i]*cos(argumento) - in_img[i]*sin(argumento);
  107.                 out_img[i] = in_real[i]*sin(argumento) + in_img[i]*cos(argumento);
  108.             }
  109.  
  110.  
  111.             ++k;
  112.             if(k==fft_length){
  113.                 k=0;
  114.             }
  115.         }
  116.  
  117.         // Tell runtime system how many input items we consumed on
  118.         // each input stream.
  119.         consume_each (noutput_items);
  120.  
  121.         // Tell runtime system how many output items we produced.
  122.         return noutput_items;
  123.     }
  124.  
  125.         void fractional_delay_impl::set_delay(float delay)
  126.     {
  127.         this->delay=delay;
  128.     }
  129.  
  130.  
  131.   } /* namespace howto */
  132. } /* namespace gr */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement