dft_delay_T.cpp
上传用户:jtjnyq9001
上传日期:2014-11-21
资源大小:3974k
文件大小:6k
源码类别:

3G开发

开发平台:

Visual C++

  1. //
  2. //  File = cpx_dft_delay.cpp
  3. //
  4. #include <stdlib.h>
  5. #include <fstream>
  6. #include "parmfile.h"
  7. #include "dft_delay_T.h"
  8. #include "misdefs.h"
  9. #include "model_graph.h"
  10. #include "sigplot.h"
  11. #include "dit_pino_T.h"
  12. #include "dit_nipo_T.h"
  13. #include "complex_io.h"
  14. extern ParmFile *ParmInput;
  15. extern SignalPlotter SigPlot;
  16. extern int PassNumber;
  17. //ofstream CorrFile("corr_res.txt", ios::out);
  18. //======================================================
  19. template< class T >
  20. DftDelay< T >::DftDelay( char* instance_name,
  21.                     PracSimModel* outer_model,
  22.                     Signal< T >* in_sig,
  23.                     Signal< T >* out_sig)
  24.             :PracSimModel(instance_name,
  25.                           outer_model)
  26. {
  27.    ENABLE_MULTIRATE;
  28.    In_Sig = in_sig;
  29.    Out_Sig = out_sig;
  30.    OPEN_PARM_BLOCK;
  31.    GET_INT_PARM(Fft_Size);
  32.    GET_DOUBLE_PARM(Dt_For_Fft);
  33.    GET_FLOAT_PARM(Overlap_Save_Mem);
  34.    GET_DOUBLE_PARM(K_Shift);
  35.    Num_Saved_Samps = int(Overlap_Save_Mem/Dt_For_Fft + 0.5);
  36.    Block_Size = Fft_Size - Num_Saved_Samps;
  37.    MAKE_OUTPUT(Out_Sig);
  38.    MAKE_INPUT(In_Sig);
  39.    SET_SAMP_INTVL(In_Sig, Dt_For_Fft);
  40.    SET_BLOCK_SIZE(In_Sig, Block_Size);
  41.    SET_SAMP_INTVL(Out_Sig, Dt_For_Fft);
  42.    SET_BLOCK_SIZE(Out_Sig, Block_Size);
  43.    //SET_DELAY( In_Sig, Out_Sig, Group_Delay_Offset);
  44. };
  45. template< class T >
  46. DftDelay< T >::~DftDelay( void )
  47. {
  48.    if(Full_Buffer != NULL) delete []Full_Buffer;
  49.    if(Adj_Resp != NULL) delete []Adj_Resp;
  50.    delete []Pt_Data_Fname;
  51.    delete []Resid_Data_Fname;
  52.    delete []Stretched_Data_Fname;
  53. };
  54. template< class T >
  55. void DftDelay< T >::Initialize(void)
  56. {
  57.    int i;
  58.    double tmp_nsexp, frac_part, int_part;
  59.    //double k_shift = 22.25;
  60.    //PointDataFile input_file;
  61.    std::complex<float> exponent;
  62.    std::complex<float> pseudo_complex;
  63.    tmp_nsexp = log10(double(Fft_Size))/log10(2.0);
  64.    frac_part = modf(tmp_nsexp, &int_part);
  65.    //------------------------------------------------------------
  66.    //  initialize derived parameters
  67.    Ns_Exp = int_part;
  68.    Full_Buffer = new std::complex<float>[Fft_Size];
  69.    for(i=0; i<Fft_Size; i++)
  70.    {
  71.       Full_Buffer[i] = std::complex<float>(0.0,0.0);
  72.    }
  73.    Adj_Resp = new std::complex<float>[Fft_Size];
  74.    //ofstream* resp_file = new ofstream("freq_resp.txt", ios::out);
  75.    for(i=0; i<Fft_Size; i++)
  76.    {
  77.       //*Pt_Data_File >> pseudo_complex;
  78.       Adj_Resp[i] = std::complex<float>(
  79.                            float(cos(TWO_PI*i*K_Shift/Fft_Size)),
  80.                            float(-sin(TWO_PI*i*K_Shift/Fft_Size)));
  81.       Adj_Resp[i] /= double(Fft_Size);
  82.       //*resp_file << i << ", " << time_resp[i] << ", "
  83.       //      << std::abs(time_resp[i]) << ", " << std::arg(time_resp[i])<< endl;
  84.    }
  85. //exit(44);
  86.    return;
  87. }
  88. template< class T >
  89. int DftDelay< T >::Execute()
  90. {
  91.    int is, ns_exp, fft_size, i;
  92.    int block_size, num_saved_samps;
  93.    T *in_sig_ptr;
  94.    T *out_sig_ptr;
  95.    //-------------------------------------------------------
  96.    //  Copy frequently accessed member vars into local vars
  97.    block_size = Block_Size;
  98.    num_saved_samps = Num_Saved_Samps;
  99.    ns_exp = Ns_Exp;
  100.    //----------------------------------------
  101.    // Get pointers for input and output
  102.    in_sig_ptr = GET_INPUT_PTR(In_Sig);
  103.    out_sig_ptr = GET_OUTPUT_PTR(Out_Sig);
  104.    //--------------------------------------
  105.    //memcpy(Full_Buffer, in_sig_ptr, Block_Size*sizeof(std::complex<float>));
  106.    for(i=0; i<Block_Size; i++)
  107.    {
  108.       Full_Buffer[i] = std::complex<float>(in_sig_ptr[i],0.0);
  109.    }
  110.    if(PassNumber == 2)
  111.    {
  112.       ofstream *pass_in_file = new ofstream("FftInput_2.txt",ios::out);
  113.       int ii;
  114.       for(ii=Block_Size; ii<Fft_Size; ii++)
  115.       {
  116.          //*pass_in_file << ((PassNumber-2)*Fft_Size+ii) << ", " << Full_Buffer[ii] << endl;
  117.          *pass_in_file << (ii - Fft_Size) << ", " << Full_Buffer[ii] << endl;
  118.       }
  119.       for(ii=0; ii<Fft_Size; ii++)
  120.       {
  121.          //*pass_in_file << ((PassNumber-1)*Fft_Size+ii) << ", " << Full_Buffer[ii] << endl;
  122.          *pass_in_file << ii << ", " << Full_Buffer[ii] << endl;
  123.       }
  124.       pass_in_file->close();
  125.       //exit(77);
  126.    }
  127.    // transform block of input samples
  128.    FftDitNipo( Full_Buffer, Fft_Size);
  129.    // multiply by sampled frequency response
  130.    for( is=0; is<Fft_Size; is++)
  131.    {
  132.       Full_Buffer[is] *= Adj_Resp[is];
  133.    }
  134.    // transform back to time domain
  135.    IfftDitNipo( Full_Buffer, Fft_Size);
  136.    // copy results to output buffer
  137.    //memcpy(out_sig_ptr, Full_Buffer, Block_Size*sizeof(std::complex<float>));
  138.    for(i=0; i<Block_Size; i++)
  139.    {
  140.       out_sig_ptr[i] = Full_Buffer[i].real();
  141.    }
  142.    //if(PassNumber == 2)
  143.    //{
  144.    //   ofstream *sing_file = new ofstream("FftOutput_2.txt",ios::out);
  145.    //   for(int ii=0; ii<Fft_Size; ii++)
  146.    //   {
  147.    //      //*sing_file << ((PassNumber-1)*46+ii) << ", " << Full_Buffer[ii] << endl;
  148.    //      *sing_file << ii << ", " << Full_Buffer[ii] << endl;
  149.    //   }
  150.    //   sing_file->close();
  151.    //   exit(77);
  152.    //}
  153.    if(Block_Size >= Num_Saved_Samps)
  154.    {
  155.       //memcpy(  &Full_Buffer[Block_Size],
  156.       //         &in_sig_ptr[block_size-num_saved_samps],
  157.       //         num_saved_samps*sizeof(std::complex<float>));
  158.       for(i=0;i<num_saved_samps; i++)
  159.       {
  160.          Full_Buffer[Block_Size+i] = in_sig_ptr[block_size-num_saved_samps+i];
  161.       }
  162.    }
  163.    else
  164.    {
  165.       memcpy(  &Full_Buffer[Block_Size],
  166.                &in_sig_ptr[2*block_size],
  167.                (num_saved_samps-block_size)*sizeof(std::complex<float>));
  168.       memcpy(  &Full_Buffer[num_saved_samps],
  169.                in_sig_ptr,
  170.                block_size*sizeof(std::complex<float>));
  171.    }
  172.    //---------------------
  173.    return(_MES_AOK);
  174. }
  175. template DftDelay< float >;