Jump to content

can we input a stimuli from a text file ?


Recommended Posts

Hello All, 

I have a problem regarding Input stimuli. I have a text file, In which I have written an AC signal.

 

Text file example:

 

Time              output voltage (volt)

0   us             2.0 v

10 us             2.1 v

20 us             2.2 v

.

.

.

100 us          3.0 v

 

and so on.....

 

I want to use this file, more specifically the "output voltage" signal as an input stimuli to a SystemC-AMS code say for example an amplifier or an A2D converter. 

 

in SystemC-AMS code, I do have a signal      sca_tdf::sca_signal<double> in;

 

how can i assign the output voltage in the text file to a "in" signal ?

how can i handle time intricacies ?

 

thanks in advance !!!

regards, 

 

Milind

Link to comment
Share on other sites

Hello Milind,

 

you cannot directly assign the stimuli values from a file to a TDF signal. You will have to implement a source module, which opens and reads the file using, e.g., the standard <fstream> library. If the time step between the samples is constant in the file, you can simply set the source's output port time step to that value and then read the next stimuli value from within the processing() callback and write it to the output port.

 

If the time step varies, you can either use the new Dynamic TDF features or you can generate the stimuli by implementing a DE source (SC_MODULE), which has an SC_THREAD, which alway waits for the next time step after it has written the last value. The DE signal can be then connected via a TDF converter port of type sca_tdf::sca_de::sca_in<double> to your TDF model.

 

You can extend both approaches to implement, e.g., interpolation between two samples.

 

You might also want to have a look to the TU Vienna SystemC AMS Building Block library available from:

 

http://www.systemc-ams.org/BB_library.html

 

Regards, Torsten

Link to comment
Share on other sites

Hello Torsten, 

 

as per the above discussion, I have written the code for a dummy source as follows, I might need ur kind comments.  (code runs with out compilation errors)

 
 
#include<systemc-ams>
#include<systemc>
 
#include<iostream.h>
#include<fstream.h>
 
using namespace std;
 
SCA_TDF_MODULE (dummy_src)
 
{
  sca_tdf :: sca_out<double> output; 
  ifstream infile;
  double val;
 
  dummy_src(sc_core::sc_module_name nm, sca_core::sca_time Tm_ = sca_core::sca_time(25, sc_core::SC_US)): output("output"), Tm(Tm_){}
 
    void set_attribute()
    {
      set_timestep;
    }
 
    void processing ()
    {
      infile.open("../datalog.txt");
      while (infile >> val)
{
 output.write(val);
 sc_core::wait(25, sc_core::SC_US); //user guide page: 38, top block
}      
    }
 
 private:
    sca_core::sca_time Tm;
};
Link to comment
Share on other sites

Hello Milind,

 

your dummy_src won't work as you're mixing in it DE and TDF semantics! In the context of TDF module, you are not at all allowed to call sc_core::wait()! Instead of the while loop in your processing() callback, use something like:

 

if (infile >> val) {

  output.write(val);

} else {

  output.write(0.0);

}

 

The time distance between the output samples is defined by the module timestep, which you specified in set_attributes().

 

Regards, Torsten

Link to comment
Share on other sites

Hello Torsten, 

In the above code context, I have written following codes and encountered an error message. could you please help me to locate the mistake?

Codes are as follows:

 

-- datalog.txt --

 

3.000315

3.000944
3.001572
3.002199
3.002829
3.003457
3.004085
3.004714
3.005342
3.005970
3.006599
3.007227
3.007855
3.008483
3.009112
3.009740
3.010368
3.010997
3.011625
3.012253
3.012881
3.013510
3.014138
3.014766
3.015395
...cont...

 

-- dummy_source.h --

 

#include<systemc-ams>
#include<systemc>
 
#include<iostream.h>
#include<fstream.h>
 
using namespace std;
 
SCA_TDF_MODULE (dummy_src)
 
{
  sca_tdf :: sca_out<double> output; 
  //  ifstream infile;
  //  double val;
 
  dummy_src(sc_core::sc_module_name): output("output"){}
 
    void set_attribute()
    {
      set_timestep(25, sc_core::SC_US);
    }
 
    void processing ()
    {
      ifstream infile;
      double val;
      infile.open("datalog.txt");
      
      if (infile >> val) {
output.write(val);
      else {
output.write(0.0);
}
      
    }
    
 };
 
-- dummy_test.cpp --
 
#include<systemc-ams.h>
#include<systemc.h>
#include<dummy_source.h>
using namespace std;
 
SC_MODULE(top_level)
{
  dummy_src vtg_src;
  sca_eln::sca_tdf::sca_vsource vin;
 
  top_level(sc_core::sc_module_name): vtg_src("vtg_src"), vin("vin"), n1("n1"), n2("n2"), gnd("gnd")
    {
      vtg_src.output(n1);
 
      vin.inp(n1);
      vin.p(n2);
      vin.n(gnd);
    }
 public: 
  sca_tdf::sca_signal<double> n1;
  sca_eln::sca_node n2;
  sca_eln::sca_node_ref gnd;
 
};
 
 
int sc_main(int argc, char* argv[])
{
  
  top_level dut("dut");
 
  sca_util :: sca_trace_file* atfs = sca_util :: sca_create_tabular_trace_file("datalog.dat");
  sca_util :: sca_trace(atfs, dut.n1, "out");
  sc_core::sc_start(0.5, sc_core::SC_SEC);
  sca_util :: sca_close_tabular_trace_file (atfs);
 
  return 0;
  
}
 
-- Error Message --
 
Error: SystemC-AMS: Error at least one sample period must be assigned per cluster 
the following modules are included in the current cluster (max. 20 printed):
dut.vtg_src
sca_linear_solver_0 containing modules:
dut.vin
 
I tried out changing set_timestep to output.set_timestep, but the error persists !!!
 
regards, 
Milind.
Link to comment
Share on other sites

   void set_attribute()

 

The function to set attributes for TDF MoC elaboration is called set_attributes() (plural form). You should be able to find such an error quite quickly by either adding simple debugging messages ("printf-debugging") or by using a proper debugger.

 

Greetings from Oldenburg,

  Philipp

Link to comment
Share on other sites

  • 5 years later...

Hi everyone

I have the same problem as milind. I did what you recommended to set  the timestep, but it just reads the first line in the text file.
if I used While (infile >> val) then at the zero time it reads all the lines and then just writes the last line to the output.
May I ask you to help me?

Thanks

Link to comment
Share on other sites

The code snippet listed above shows that the file is repeatedly opened in the processing() method, since this method is called at each time step. Instead, the file should only be opened once, e.g. in the module constructor or initialize() callback. Note that in this case you need to make the variable of type ifstream a (private) member of the class, so other methods can access this variable.

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...