Jump to content

Capture sine waves having different characteristics using timestep


Recommended Posts

I have two TDF modules param_gen and func. The function of param_gen is to write combination of amplitude and frequency values to its output port and func module reads those values and generate sine wave according to the characteristics read. My goal is to  generate sine waves from the combination of amplitude and frequency values inserted.

But the problem I'm facing is signal generated are no longer sine waves because of changing amplitude and frequency values. I tried capturing those signals using longer timestep so that they atleast look sine waves but that does not help. I'm pasting the code below. All kind of suggestions would be helpful.

 

I have an array initilised with amplitude and frequency values in the param_gen.h file.

double combi[4][2]=     //combi(amp,freq)
         {
                 {1,1e12},
                 {1.5,5e12},
                 {2,10e12},
                 {2.5,15e12},

         };

Now in param_gen.cpp file, in function processing(), I write new values to the output port output_amp after a considerable amount of time step

 

void param_gen::processing()
{

double current_time=get_time().to_seconds();

double time_difference=get_timestep().to_seconds()-last_timestep;

    if (time_difference==5e-6)

    {
        std::cout<<counter<<"   "<<"counter"<<endl;
        output_amp.write(combi[index1+1][0]);   //update new values
        output_freq.write(combi[index1+1][1]);
          index1++;
         if(index1==3)
         {
             index1=0;   // reset the index so that combi values repeat again for remaining cycles

         }

    }


    else
    {

        output_amp.write(combi[index1][0]);  // otherwise write the old values
         output_freq.write(combi[index1][1]);
         last_timestep=current_time;
         

    }
}

void param_gen::set_attributes_cpp()
{
    output_amp.set_timestep(sca_core::sca_time(1,sc_core::SC_FS));
   output_freq.set_timestep(sca_core::sca_time(1,sc_core::SC_FS));   

}

The func module simply read those values and generate sine singlas.

void func::processing()
{
    double t = single_sine_src_out.get_time().to_seconds();
    double t1 = double_sine_src_out.get_time().to_seconds();
    double t2 = triple_sine_src_out.get_time().to_seconds();

    std::cout<<amp_in.read()<<"   "<<" amp value "<<endl;
    std::cout<<freq_in.read()<<"   "<<"freq value"<<endl;

    double x=amp_in.read()* sin ((2 * M_PI * freq_in.read() * t)+ p.phase1);
    double x1 = x + (amp_in.read() * sin ((2 * M_PI *freq_in.read()+100 * t1) + p.phase2));
    double x2 = x1 + (amp_in.read() * sin ((2 * M_PI * freq_in.read()+200 * t2) + p.phase3));// ! 100,200 replace with variables

    single_sine_src_out.write(x);
    double_sine_src_out.write(x1);
    triple_sine_src_out.write(x2);

}

void func::set_attributes_cpp()
{
    amp_in.set_timestep(sca_core::sca_time(1,sc_core::SC_FS));
    freq_in.set_timestep(sca_core::sca_time(1,sc_core::SC_FS));
  }

I'm attaching my schematic below for a clear image of the problem.

Screenshot (76).png

Link to comment
Share on other sites

You don't provide a minimal self-contained code example that exposes your problem. This complicates for an external the analysis of your problem. Also, a plot of the relevant signals would help to understand better, what wrong behaviour you are observing. Anyway, the calculation of the sine argument for x1 and x2 look suspicious to me: The "+" in front of 100 and 200 should probably be a "*" so that it corresponds to the general form of a wave equation x = A * sin(omega * t + phi).

Also, it is wasteful to recalculate the amplitudes and frequencies in param_gen at each time step. If frequency and phase change discretely with a fixed time step, you may consider to use TDF's multirate feature by simply setting the source outputs in the func block to a much higher rate. Alternatively, you may convert your param_gen block to a SystemC module and use sca_tdf::sca_de::sca_in<double> converter ports for free_in and amp_in. In case you later plan to change amplitude and frequency continuously at the time step used for the wave output, this won't be possible though.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...