50 V
mod(
const V& a,
const V& b)
52 return (a % b + b) % b;
69 time_trace_ffmt(
bool num_samples_on_,
bool header_on_, std::string header_, std::string col_sep_)
73 time_trace_ffmt(
bool num_samples_on_,
bool header_on_, std::string header_, std::string col_sep_,
int fmt_id_)
90 std::streampos start_of_data = inpf.beg;
94 std::getline(inpf,line);
97 std::size_t found_hash = line.find_first_of(
"#");
98 if(found_hash != std::string::npos)
101 start_of_data = inpf.tellg();
107 size_t items = sscanf(line.c_str(),
"%lf %lf", &t, &f);
113 size_t item = sscanf(line.c_str(),
"%d", &num_samples);
130 inpf.seekg(start_of_data,inpf.beg);
168 time_trace(_duration, _dt, _rtOff, _label,
"",
"")
180 int N = ceil(_duration/
dt) + 1;
186 for(
size_t i=0; i<
t.
size(); i++) {
239 void set_labels(std::string _label, std::string _t_unit, std::string _f_unit)
245 void setUnits(std::string _t_unit, std::string _f_unit)
270 for(std::size_t i=0; i<
len(); i++)
281 for(std::size_t i=0; i<
len(); i++)
298 for(std::size_t i=0; i<
len()-1; i++)
299 f[i] =
f[i+1] -
f[i];
322 dFdt[0] = (
f[1] -
f[0]) / (
t[1] -
t[0]);
323 dFdt[end] = (
f[end] -
f[end-1]) / (
t[end] -
t[end-1]);
326 for(
int i=1; i<end-1; i++)
328 sReal fdF = (
f[i ] -
f[i-1]) / (
t[i ] -
t[i-1]);
329 sReal bdF = (
f[i+1] -
f[i ]) / (
t[i+1] -
t[i ]);
331 dFdt[i] = (fdF + bdF) * 0.5;
369 for (std::size_t i=1;i<
len();i++) {
383 sReal unsc = fabs(_mx)>=fabs(_mn) ? 1./fabs(_mx):1./fabs(_mn);
385 for (std::size_t i=0;i<
len();i++)
404 sReal r_err = 0.001*dt_o;
406 for (std::size_t i=1; i<
len()-1; i++) {
408 if ( (dt_n - dt_o) > r_err)
420 return fade ? 0. :
f[0];
426 while(
t[i] <= _t) i++;
428 sReal K = (
f[i] -
f[i-1]) / (
t[i] -
t[i-1]);
429 sReal fval =
f[i-1] + K * (_t -
t[i-1]);
436 for(std::size_t i=0; i<trc.
len(); i++)
460 for (std::size_t i=0; i<trc.
len(); i++) {
466 if ( (_t <
t[0]) || (_t >
t.
back()) )
483 trc.
f[i] =
f[i1] + (
f[i2]-
f[i1])/(
t[i2]-
t[i1])*(_t-
t[i1]);
486 if ((trc.
t[i]-
t[i1]) >= (
t[i2]-trc.
t[i]))
501 assert(
len() == a.
len() );
503 for(
size_t i=0; i<
len(); i++)
510 for(
auto & v :
f) v *= s;
513 inline void time_trace::time_trace::operator += (
const sReal a)
515 for(
auto & v : f) v += a;
521 assert(
len() == a.
len() );
523 for(
size_t i=0; i<
len(); i++)
529 bool ring = delta < 0;
537 for(
int i=0; i<N; i++)
538 f[
mod((i-delta),N)] = tmp[i];
542 for(std::size_t i=
len()-delta;i<
len();i++)
548 bool ring = delta < 0;
556 for(
int i=0; i<N; i++)
557 f[
mod((i+delta),N)] = tmp[i];
561 for(
int i=0;i<delta;i++)
569 std::ostringstream sheader_;
574 sheader_ <<
"# " << ffmt_spec.
header << std::endl;
577 sheader_ <<
"# version = " << ffmt_spec.
fmt_id << std::endl;
580 sheader_ <<
"#" << std::endl;
581 sheader_ <<
"# TABLE_HEAD" << std::endl;
582 sheader_ <<
"# A = time" << std::endl;
583 sheader_ <<
"# B = " <<
label << std::endl;
584 sheader_ <<
"# [" << std::setw(ffmt_spec.
colw[0]) <<
"A" <<
"]";
585 sheader_ <<
"[" << std::setw(ffmt_spec.
colw[1]) <<
"B" <<
"]" << std::endl;
586 sheader_ <<
"# [" << std::setw(ffmt_spec.
colw[0]) <<
t_unit <<
"]";
587 sheader_ <<
"[" << std::setw(ffmt_spec.
colw[1]) <<
f_unit <<
"]" << std::endl;
588 sheader_ <<
"#" << std::endl;
589 sheader_ <<
"# DATA" << std::endl;
594 sheader_ <<
len() << std::endl;
596 std::string sheader = sheader_.str();
627 std::ifstream inpf(fname);
633 std::cerr <<
"Trace file " << fname <<
" could not be opened for reading!" << std::endl;
644 if(tt_ffmt.num_samples_on)
654 while( inpf >>
t[i] >>
f[i] )
657 if(tt_ffmt.num_samples_on)
659 if(i<numSamples || i<2)
737 std::ofstream outf(fname);
742 std::cerr <<
"Trace file " << fname <<
" could not be opened for writing!" << std::endl;
754 outf <<
len() << std::endl;
759 for(std::size_t i=0; i<
len(); i++)
761 outf << std::setw(ffmt_spec.
colw[0]+3) << std::setprecision(3) <<
t[i] << ffmt_spec.
col_sep;
762 outf << std::setw(ffmt_spec.
colw[1]+1) << std::setprecision(6) <<
f[i] << std::endl;
773 assert( a.
len() == b.
len() );
777 for(std::size_t i=0; i<a.
len(); i++)
778 _mult.
f[i] = a.
f[i] * b.
f[i];
785 assert( a.
len() == b.
len() );
789 for(std::size_t i=0; i<a.
len(); i++)
790 _div.
f[i] = a.
f[i] / b.
f[i];
797 assert( a.
len() == b.
len() );
800 for(std::size_t i=0; i<a.
len(); i++)
801 _add.
f[i] = a.
f[i] + b.
f[i];
808 assert( a.
len() == b.
len() );
811 for(std::size_t i=0; i<a.
len(); i++)
812 _sub.
f[i] = a.
f[i] - b.
f[i];
919 for(std::size_t i=0; i<trc.
len(); i++)
944 for(std::size_t i=0; i<trc.
t.
size(); i++)
964 for(std::size_t i=0; i<trc.
len(); i++)
983 for(std::size_t i=0; i<trc.
len(); i++)
1011 for(std::size_t i=0; i<trc.
t.
size(); i++)
1026 stepPars.
rise =
false;
1038 int i1 = int(t1/trc.
dt);
1039 sReal f0 = phase_0.
f[i1];
1040 for(std::size_t i=i1; i<trc.
len(); i++)
1044 time_trace pulse = phase_0 * eps_0 + phase_1 * eps_1;
The vector class and related algorithms.
size_t size() const
The current size of the vector.
void resize(size_t n)
Resize a vector.
const T * end() const
Pointer to the vector's end.
void assign(InputIterator s, InputIterator e)
Assign a memory range.
const T * begin() const
Pointer to the vector's start.
float s2r
strength of subpulse relative to leading pulse (biphasic pulse)
sReal duration
pulse duration, default is 1 ms
float tau_plat
time constant governing plateau of pulse
float tau_edge
time constant for leading/trailing edges
float bias
constant term to add to stimulus waveform
bool constant
constant value for all time
float strength
pulse amplitude, default is unit strength
double d1
duration of first sub-pulse in [ms] (zero with monophasic pulse)
biphasic truncated exponentials (capacitive discharge)
virtual SF::vector< sReal > & sample(time_trace &trc)
biphasicTruncExpFunc(Pulse _pars)
virtual SF::vector< sReal > & sample(time_trace &trc)
monophasic truncated exponentials (capacitive discharge)
virtual SF::vector< sReal > & sample(time_trace &trc)
monophasicTruncExpFunc(Pulse _pars)
virtual SF::vector< sReal > & sample(time_trace &trc)=0
virtual SF::vector< sReal > & sample(time_trace &trc)
sineWave(sReal _frq, sReal _phase)
float phase
phase in degree
virtual SF::vector< sReal > & sample(time_trace &trc)
bool header_on
turn on header output
time_trace_ffmt(bool num_samples_on_, bool header_on_, std::string header_, std::string col_sep_, int fmt_id_)
bool num_samples_on
turn on # of samples output
int parse_header(std::ifstream &inpf)
parse trace header from file
time_trace_ffmt(bool num_samples_on_, bool header_on_, std::string header_, std::string col_sep_)
std::string col_sep
column separator, either " " or ","
SF::vector< int > colw
width of columns
int fmt_id
format identifier
void set_labels(std::string _label)
time_trace(sReal _dt)
prescribe dt only, default duration is 1000. ms
void resize(int numSamples)
time_trace(sReal _duration, sReal _dt)
prescribe duration and sampling, use default real time offset
bool fade
signal fades to zero after end or remains at final amplitude
time_trace(sReal _duration, sReal _dt, sReal _rtOff, std::string _label, std::string _t_unit, std::string _f_unit)
full constructor, prescribe all members
bool IsEquDistSampling()
check whether time trace is sampled with constant sampling interval
void resample(time_trace &trc, IpMeth_t meth)
sReal fval_t(sReal _t)
retrieve value of a function at a given time t
void interp1(time_trace &trc, IpMeth_t meth)
interpolate a time trace
std::string label
descriptive label of signal
void unitize()
scale signal to unity strength
void set_labels(std::string _label, std::string _t_unit, std::string _f_unit)
time_trace(sReal _duration, sReal _dt, sReal _rtOff, std::string _label)
SF::vector< sReal > t
time axis
SF::vector< sReal > & gradient()
void resample(time_trace &trc)
std::string format_file_header(const time_trace_ffmt ffmt_spec)
IO format file header from format specifciation and given time trace.
size_t timeShift(sReal t_shift)
void operator<<=(int delta)
SF::vector< sReal > & diff()
std::string t_unit
unit of time axis
sReal dt
temporal discretization of trace, if regularly sampled
time_trace(sReal _duration, sReal _dt, sReal _rtOff)
SF::vector< sReal > f
store function values of trace
time_trace(const time_trace &a, sReal val)
copy constructor with setting of constant signal value
sReal rtOff
real time offset of time axis
std::string f_unit
unit of signal
void setUnits(std::string _t_unit, std::string _f_unit)
int write_trace()
write traces to file
time_trace & operator=(const time_trace &in)
void operator>>=(int delta)
int read_trace(const std::string fname)
determine duration of a signal stored in file
void operator*=(const time_trace &a)
overloaded operators
size_t len() const
< length of trace in samples
time_trace()
use defaults only, 1000 ms duration, 10 us sampling interval, no real time offset
time_trace(const time_trace &a)
copy constructor with setting of constant signal value
void operator+=(const sReal a)
V mod(const V &a, const V &b)
time_trace operator*(const time_trace &a, const time_trace &b)
time_trace operator+(const time_trace &a, const time_trace &b)
time_trace operator-(const time_trace &a, const time_trace &b)
time_trace operator/(const time_trace &a, const time_trace &b)
enum opencarp::sig::ip_method IpMeth_t