diff --git a/SleepLib/session.cpp b/SleepLib/session.cpp index a1bfc8e5..d5c0b608 100644 --- a/SleepLib/session.cpp +++ b/SleepLib/session.cpp @@ -643,33 +643,53 @@ void Session::updateCountSummary(ChannelID code) QHash >::iterator ev=eventlist.find(code); if (ev==eventlist.end()) return; + QHash >::iterator vs=m_valuesummary.find(code); + if (vs!=m_valuesummary.end()) // already calculated? + return; + QHash valsum; QHash timesum; - QHash::iterator vsi; - QHash::iterator tsi; + //QHash::iterator vsi; + //QHash::iterator tsi; EventDataType raw,lastraw=0; - qint64 time,lasttime=0; - qint32 len; + qint64 start,time,lasttime=0; + qint32 len,cnt; + quint32 * tptr; + EventStoreType * dptr; for (int i=0;i0) { - len=(time-lasttime) / 1000; - } else len=0; + //raw=e.raw(j); + valsum[raw]++; +// vsi=valsum.find(raw); +// if (vsi==valsum.end()) { +// valsum[raw]=1; +// } +// else vsi.value()++; - tsi=timesum.find(lastraw); - if (tsi==timesum.end()) timesum[raw]=len; - else tsi.value()+=len; + //time=e.time(j); + + // elapsed time in seconds since last event + len=(time-lasttime) / 1000L; + + timesum[lastraw]+=len; +// tsi=timesum.find(lastraw); +// if (tsi==timesum.end()) timesum[raw]=len; +// else tsi.value()+=len; lastraw=raw; lasttime=time; @@ -899,13 +919,19 @@ int Session::rangeCount(ChannelID id, qint64 first,qint64 last) return 0; } QVector & evec=j.value(); - int sum=0; + int sum=0,cnt; - qint64 t; + qint64 t,start; + quint32 * tptr; for (int i=0;i=first) && (t<=last)) { sum++; } @@ -920,16 +946,26 @@ double Session::rangeSum(ChannelID id, qint64 first,qint64 last) return 0; } QVector & evec=j.value(); - double sum=0; + double sum=0,gain; + + qint64 t,start; + EventStoreType * dptr; + quint32 * tptr; + int cnt; - qint64 t; for (int i=0;i=first) && (t<=last)) { - sum+=ev.data(j); + sum+=*dptr * gain; } + dptr++; } } return sum; @@ -941,18 +977,28 @@ EventDataType Session::rangeMin(ChannelID id, qint64 first,qint64 last) return 0; } QVector & evec=j.value(); - EventDataType v,min=999999999; + EventDataType gain,v,min=999999999; + + qint64 t,start; + EventStoreType * dptr; + quint32 * tptr; + int cnt; - qint64 t; for (int i=0;i=first) && (t<=last)) { - v=ev.data(j); + v=EventDataType(*dptr) * gain; if (v & evec=j.value(); - EventDataType v,max=-999999999; + EventDataType gain,v,max=-999999999; + + qint64 t,start; + EventStoreType * dptr; + quint32 * tptr; + int cnt; - qint64 t; for (int i=0;i=first) && (t<=last)) { - v=ev.data(j); + v=EventDataType(*dptr) * gain; if (v>max) max=v; } + dptr++; } } return max; @@ -1013,10 +1069,17 @@ double Session::sum(ChannelID id) } QVector & evec=j.value(); - double sum=0; + double gain,sum=0; + EventStoreType * dptr; + int cnt; for (int i=0;icount();j++) { - sum+=evec[i]->data(j); + EventList & ev=*(evec[i]); + gain=ev.gain(); + cnt=ev.count(); + dptr=ev.rawData(); + + for (int j=0;j & evec=j.value(); - double val=0; + double val=0,gain; int cnt=0; + EventStoreType * dptr; + int es; for (int i=0;icount();j++) { - val+=evec[i]->data(j); + EventList & ev=*(evec[i]); + dptr=ev.rawData(); + gain=ev.gain(); + es=ev.count(); + + for (int j=0;j::iterator i=m_90p.find(id); - if (i!=m_90p.end()) - return i.value(); - - if (!eventlist.contains(id)) { - m_90p[id]=0; - return 0; - } - - EventDataType val=percentile(id,0.9); - m_90p[id]=val; - return val; -} - -EventDataType Session::p95(ChannelID id) -{ - QHash::iterator i=m_95p.find(id); - if (i!=m_95p.end()) - return i.value(); - - if (!eventlist.contains(id)) { - m_95p[id]=0; - return 0; - } - - EventDataType val=percentile(id,0.95); - m_95p[id]=val; - return val; - -} - -EventDataType Session::median(ChannelID id) -{ - QHash::iterator i=m_med.find(id); - if (i!=m_med.end()) - return i.value(); - - if (!eventlist.contains(id)) { - m_med[id]=0; - return 0; - } - - EventDataType val=percentile(id,0.5); - m_med[id]=val; - return val; -} - -*/ bool sortfunction (EventStoreType i,EventStoreType j) { return (i >::iterator jj=eventlist.find(id); if (jj==eventlist.end()) @@ -1173,88 +1194,98 @@ EventDataType Session::percentile(ChannelID id,EventDataType percent) EventDataType Session::wavg(ChannelID id) { + if (id==CPAP_Pressure) { + int i=5; + } QHash vtime; QHash::iterator i=m_wavg.find(id); if (i!=m_wavg.end()) return i.value(); - QHash >::iterator jj=eventlist.find(id); - if (jj==eventlist.end()) - return 0; - QVector & evec=jj.value(); + updateCountSummary(id); - int size=evec.size(); - if (size==0) + QHash >::iterator j2=m_timesummary.find(id); + QHash & timesum=j2.value(); + + if (!m_gain.contains(id)) return 0; - qint64 lasttime=0,time,td; - EventStoreType val,lastval=0; + double s0=0,s1=0,s2; - - EventDataType gain=evec[0]->gain(); - EventStoreType minval; - - for (int i=0;icount()) continue; - /*lastval=evec[i]->raw(0); - lasttime=evec[i]->time(0); - for (quint32 j=1;jcount();j++) { - val=evec[i]->raw(j); - time=evec[i]->time(j); - td=(time-lasttime); - - if (vtime.contains(lastval)) { - vtime[lastval]+=td; - } else vtime[lastval]=td; - lasttime=time; - lastval=val; - }*/ - time=evec[i]->time(0)/1000L; - minval=val=evec[i]->raw(0); - for (quint32 j=1;jcount();j++) { - lastval=val; - lasttime=time; - val=evec[i]->raw(j); - if (valtime(j)/1000L; - td=(time-lasttime); - if (vtime.contains(lastval)) { - vtime[lastval]+=td; - } else vtime[lastval]=td; - } - if (lasttime>0) { - td=(last()/1000L)-time; - if (vtime.contains(val)) { - vtime[val]+=td; - } else vtime[val]=td; - - } + EventDataType val, gain=m_gain[id]; + for (QHash::iterator vi=timesum.begin();vi!=timesum.end();vi++) { + val=vi.key() * gain; + s2=vi.value(); + s0+=s2; + s1+=val * s2; } + if (s0>0) { + val=s1/s0; + } else val=0; - if (minval<0) minval=-minval; - minval++; - // if (minval<0) minval+=(0-minval)+1; else minval=1; - qint64 s0=0,s1=0,s2=0,s3=0; // 32bit may all be thats needed here.. - for (QHash::iterator i=vtime.begin(); i!=vtime.end(); i++) { - s0=i.value(); - s3=i.key()+minval; - s1+=s3*s0; - s2+=s0; - } - if (s2==0) { - return m_wavg[id]=0; - } - double j=double(s1)/double(s2); - j-=minval; - EventDataType v=j*gain; - if (v>32768*gain) { - v=0; - } - if (v<-(32768*gain)) { - v=0; - } - m_wavg[id]=v; - return v; + m_wavg[id]=val; + return val; + +// int size=evec.size(); +// if (size==0) +// return 0; + +// qint64 lasttime=0,time,td; +// EventStoreType val,lastval=0; + + +// EventDataType gain=evec[0]->gain(); +// EventStoreType minval; + +// for (int i=0;icount()) continue; + +// time=evec[i]->time(0)/1000L; +// minval=val=evec[i]->raw(0); +// for (quint32 j=1;jcount();j++) { +// lastval=val; +// lasttime=time; +// val=evec[i]->raw(j); +// if (valtime(j)/1000L; +// td=(time-lasttime); +// if (vtime.contains(lastval)) { +// vtime[lastval]+=td; +// } else vtime[lastval]=td; +// } +// if (lasttime>0) { +// td=(last()/1000L)-time; +// if (vtime.contains(val)) { +// vtime[val]+=td; +// } else vtime[val]=td; + +// } +// } + +// if (minval<0) minval=-minval; +// minval++; +// // if (minval<0) minval+=(0-minval)+1; else minval=1; +// qint64 s0=0,s1=0,s2=0,s3=0; // 32bit may all be thats needed here.. +// for (QHash::iterator i=vtime.begin(); i!=vtime.end(); i++) { +// s0=i.value(); +// s3=i.key()+minval; +// s1+=s3*s0; +// s2+=s0; +// } +// if (s2==0) { +// return m_wavg[id]=0; +// } +// double j=double(s1)/double(s2); +// j-=minval; +// EventDataType v=j*gain; +// if (v>32768*gain) { +// v=0; +// } +// if (v<-(32768*gain)) { +// v=0; +// } +// m_wavg[id]=v; +// return v; } EventList * Session::AddEventList(ChannelID code, EventListType et,EventDataType gain,EventDataType offset,EventDataType min, EventDataType max,EventDataType rate,bool second_field)