X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=common%2FclitkSignal.cxx;h=825243d16714ce74955f8129ed5df6b4a47cb789;hb=4ac333621094d8036703d4748d073d4eb66df82c;hp=9f65bbf421daa038b9deeed6f69f3e2538c4fe8b;hpb=931a42358442f4ee4f314613c991c838d4b4e3b7;p=clitk.git diff --git a/common/clitkSignal.cxx b/common/clitkSignal.cxx index 9f65bbf..825243d 100644 --- a/common/clitkSignal.cxx +++ b/common/clitkSignal.cxx @@ -2,12 +2,33 @@ #define CLITKSIGNAL_CXX #include "clitkSignal.h" +#include namespace clitk { + //--------------------------------------------------------------------- + Signal::Signal(const Signal & s) { // Copy + CopyFrom(s); + } + //--------------------------------------------------------------------- + + + //--------------------------------------------------------------------- + void Signal::CopyFrom(const Signal & s) { + SetSamplingPeriod(s.GetSamplingPeriod()); + // DD(GetSamplingPeriod()); + resize(s.size()); + for(uint i=0; i> sub; + // DD(sub); + // DD(sub.size()); + // DD(iss.bad()); + if (!iss) { + std::cerr << "ERROR: no col n" << col << " in the line '" << line << "' ?" << std::endl; + return false; //exit(0); + } } iss >> point; - if (!iss) { - std::cerr << "ERROR: no col n" << col << " in the line '" << line << "' ?" << std::endl; - exit(0); - } skipComment(signalStream); m_Data.push_back(point); // cout << point << endl; } signalStream.close(); SetSamplingPeriod(1.); + return true; } //--------------------------------------------------------------------- @@ -70,23 +95,23 @@ namespace clitk { Signal Signal::ConvertImageToSignal( Signal::ImageType::Pointer image) { //empty signal - Signal signal; + Signal signal; - //make an image iterator - itk::ImageRegionConstIterator it(image,image->GetLargestPossibleRegion()); - it.Begin(); + //make an image iterator + itk::ImageRegionConstIterator it(image,image->GetLargestPossibleRegion()); + it.Begin(); - //copy - while(!it.IsAtEnd()) - { - signal.push_back(it.Get()); - ++it; - } + //copy + while(!it.IsAtEnd()) + { + signal.push_back(it.Get()); + ++it; + } - //Spacing - signal.SetSamplingPeriod(image->GetSpacing()[0]); + //Spacing + signal.SetSamplingPeriod(image->GetSpacing()[0]); - return signal; + return signal; } //--------------------------------------------------------------------- @@ -95,41 +120,41 @@ namespace clitk { //Convert 1D signal to image Signal::ImageType::Pointer Signal::ConvertSignalToImage(Signal signal) { - //empty image - ImageType::Pointer image =ImageType::New(); - ImageType::RegionType region; - ImageType::RegionType::IndexType index; - index[0]=0; - ImageType::RegionType::SizeType size; - size[0]=signal.size(); - - region.SetIndex(index); - region.SetSize(size); - - image->SetRegions(region); - image->Allocate(); - - //make an image iterator - itk::ImageRegionIterator mIt(image,image->GetLargestPossibleRegion()); - mIt.Begin(); - - //make a signal iterator - Signal::const_iterator sIt=signal.begin(); - - //copy - while(sIt!=signal.end()) - { - mIt.Set(*sIt); - sIt++;++mIt; - } + //empty image + ImageType::Pointer image =ImageType::New(); + ImageType::RegionType region; + ImageType::RegionType::IndexType index; + index[0]=0; + ImageType::RegionType::SizeType size; + size[0]=signal.size(); + + region.SetIndex(index); + region.SetSize(size); + + image->SetRegions(region); + image->Allocate(); + + //make an image iterator + itk::ImageRegionIterator mIt(image,image->GetLargestPossibleRegion()); + mIt.Begin(); + + //make a signal iterator + Signal::const_iterator sIt=signal.begin(); + + //copy + while(sIt!=signal.end()) + { + mIt.Set(*sIt); + sIt++;++mIt; + } - //spacing - ImageType::SpacingType spacing; - spacing[0]=signal.GetSamplingPeriod(); - image->SetSpacing(spacing); + //spacing + ImageType::SpacingType spacing; + spacing[0]=signal.GetSamplingPeriod(); + image->SetSpacing(spacing); - return image; + return image; } //--------------------------------------------------------------------- @@ -188,8 +213,8 @@ namespace clitk { iterator itD; itD=mul.begin(); while(it!=end()){ - *it *= *itD; - it++;itD++; + *it *= *itD; + it++;itD++; } return *this; } @@ -315,117 +340,117 @@ namespace clitk { //--------------------------------------------------------------------- - Signal Signal::HighPassFilter (double sampPeriod, double cutOffFrequency ) - { - //output - Signal temp(m_SamplingPeriod); - temp.resize(size()); - - //the fft - SIGNAL_FFT_TYPE fft; - - //calculate the cut off frequency - unsigned int samp=lrint(cutOffFrequency*static_cast(size())*sampPeriod); - - //forward fft with empty fft - if(fft.size()==0) OneDForwardFourier(*this, fft); - - //remove the frequencies - for(unsigned int i=0;i(0.,0.); - - //backward with remaining frequencies - OneDBackwardFourier(fft,temp); - return temp; - } +// Signal Signal::HighPassFilter (double sampPeriod, double cutOffFrequency ) +// { +// //output +// Signal temp(m_SamplingPeriod); +// temp.resize(size()); +// +// //the fft +// SIGNAL_FFT_TYPE fft; +// +// //calculate the cut off frequency +// unsigned int samp=lrint(cutOffFrequency*static_cast(size())*sampPeriod); +// +// //forward fft with empty fft +// if(fft.size()==0) OneDForwardFourier(*this, fft); +// +// //remove the frequencies +// for(unsigned int i=0;i(0.,0.); +// +// //backward with remaining frequencies +// OneDBackwardFourier(fft,temp); +// return temp; +// } //--------------------------------------------------------------------- - //--------------------------------------------------------------------- - Signal Signal::LowPassFilter (double sampPeriod, double cutOffFrequency ) - { - //output - Signal temp(m_SamplingPeriod); - temp.resize(size()); - - //the fft - SIGNAL_FFT_TYPE fft; - - //calculate the cut off frequency - unsigned int samp=lrint(cutOffFrequency*static_cast(size())*sampPeriod); - - //forward fft with empty fft - if(fft.size()==0) OneDForwardFourier(*this, fft); - unsigned int fsize=fft.size(); - - //remove the frequencies - unsigned int limit=min (samp, fsize); - for(unsigned int i=limit;i(0.,0.); - - //backward with remaining frequencies - OneDBackwardFourier(fft,temp); - return temp; - } + //--------------------------------------------------------------------- +// Signal Signal::LowPassFilter (double sampPeriod, double cutOffFrequency ) +// { +// //output +// Signal temp(m_SamplingPeriod); +// temp.resize(size()); +// +// //the fft +// SIGNAL_FFT_TYPE fft; +// +// //calculate the cut off frequency +// unsigned int samp=lrint(cutOffFrequency*static_cast(size())*sampPeriod); +// +// //forward fft with empty fft +// if(fft.size()==0) OneDForwardFourier(*this, fft); +// unsigned int fsize=fft.size(); +// +// //remove the frequencies +// unsigned int limit=min (samp, fsize); +// for(unsigned int i=limit;i(0.,0.); +// +// //backward with remaining frequencies +// OneDBackwardFourier(fft,temp); +// return temp; +// } //--------------------------------------------------------------------- //--------------------------------------------------------------------- - void Signal::OneDForwardFourier(const Signal& input, SIGNAL_FFT_TYPE & fft) - { - //Create output array - fft.resize(input.size()/2+1); - //Temp copy - double *tempCopy=new double[size()]; - copy(begin(), end(), tempCopy); - - //Forward Fourier Transform - fftw_plan p; - p=fftw_plan_dft_r2c_1d(size(),tempCopy,reinterpret_cast(&(fft[0])),FFTW_ESTIMATE); - fftw_execute(p); - fftw_destroy_plan(p); - //delete tempCopy; - return; - } +// void Signal::OneDForwardFourier(const Signal& input, SIGNAL_FFT_TYPE & fft) +// { +// //Create output array +// fft.resize(input.size()/2+1); +// //Temp copy +// double *tempCopy=new double[size()]; +// copy(begin(), end(), tempCopy); +// +// //Forward Fourier Transform +// fftw_plan p; +// p=fftw_plan_dft_r2c_1d(size(),tempCopy,reinterpret_cast(&(fft[0])),FFTW_ESTIMATE); +// fftw_execute(p); +// fftw_destroy_plan(p); +// //delete tempCopy; +// return; +// } //--------------------------------------------------------------------- //--------------------------------------------------------------------- - void Signal::OneDBackwardFourier(SIGNAL_FFT_TYPE & fft, Signal &output) - { - - //Backward - fftw_plan p; - p=fftw_plan_dft_c2r_1d(output.size(),reinterpret_cast(&(fft[0])),&(output[0]),FFTW_ESTIMATE); - fftw_execute(p); - fftw_destroy_plan(p); - - vector::iterator it=output.begin(); - while(it!=output.end()){ - *it /= (double)output.size(); - it++; - } - return; - } +// void Signal::OneDBackwardFourier(SIGNAL_FFT_TYPE & fft, Signal &output) +// { +// +// //Backward +// fftw_plan p; +// p=fftw_plan_dft_c2r_1d(output.size(),reinterpret_cast(&(fft[0])),&(output[0]),FFTW_ESTIMATE); +// fftw_execute(p); +// fftw_destroy_plan(p); +// +// vector::iterator it=output.begin(); +// while(it!=output.end()){ +// *it /= (double)output.size(); +// it++; +// } +// return; +// } //--------------------------------------------------------------------- //--------------------------------------------------------------------- - double Signal::MaxFreq(const Signal &sig,SIGNAL_FFT_TYPE & fft) - { - - if(fft.size()==0) OneDForwardFourier(sig,fft); - int posMax=1; - double amplitude, amplitudeMax=abs(fft[1]); - for(unsigned int i=1;iamplitudeMax){ - posMax=i; - amplitudeMax=amplitude; - } - } - return ((double)(posMax)/((double)sig.size()*sig.GetSamplingPeriod())); - } +// double Signal::MaxFreq(const Signal &sig,SIGNAL_FFT_TYPE & fft) +// { +// +// if(fft.size()==0) OneDForwardFourier(sig,fft); +// int posMax=1; +// double amplitude, amplitudeMax=abs(fft[1]); +// for(unsigned int i=1;iamplitudeMax){ +// posMax=i; +// amplitudeMax=amplitude; +// } +// } +// return ((double)(posMax)/((double)sig.size()*sig.GetSamplingPeriod())); +// } //--------------------------------------------------------------------- @@ -484,7 +509,7 @@ namespace clitk { //Search the first value in extrema not 0.5 while(m_Data[beginCycle]!=1) { - beginCycle++; + beginCycle++; } //We search the corresponding end @@ -492,7 +517,7 @@ namespace clitk { while(endCycle != size() && m_Data[endCycle]!=1){ endCycle++; - } + } //============================================================ //Calculate phase at the beginning (before the first extremum) @@ -547,7 +572,7 @@ namespace clitk { //the last part is shorter, copy the last cycle values else{ phase[i] = phase[i -(endCycle-beginCycle)]; - } + } } //=================================================================== @@ -581,7 +606,7 @@ namespace clitk { //Search the first value in extrema not 0.5 while(m_Data[beginCycle]!=1) { - beginCycle++; + beginCycle++; } //We search the corresponding end @@ -589,7 +614,7 @@ namespace clitk { while(endCycle != size() && m_Data[endCycle]!=1){ endCycle++; - } + } //=================================================================== //Calculate phase at the beginning (before the first extremum) @@ -645,7 +670,7 @@ namespace clitk { //the last part is shorter, copy the last cycle values else{ phase[i] = phase[i -(endCycle-beginCycle)]+1; - } + } } //=================================================================== @@ -676,11 +701,11 @@ namespace clitk { iterator monPhaseIt=monPhase.begin(); iterator extremaIt =begin(); while (extremaIt!= end()) - { - if (*extremaIt==0.) *phaseIt=eIPhaseValue+floor(*monPhaseIt); - else if (*extremaIt==1.) *phaseIt=eEPhaseValue+floor(*monPhaseIt); - extremaIt++; phaseIt++;monPhaseIt++; - } + { + if (*extremaIt==0.) *phaseIt=eIPhaseValue+floor(*monPhaseIt); + else if (*extremaIt==1.) *phaseIt=eEPhaseValue+floor(*monPhaseIt); + extremaIt++; phaseIt++;monPhaseIt++; + } return phase; @@ -761,6 +786,30 @@ namespace clitk { //--------------------------------------------------------------------- + +// //--------------------------------------------------------------------- +// Signal Signal::ApproximateScatteredValuesWithBSplines(unsigned int splineOrder, unsigned int numberOfControlPoints) +// { +// //filter requires a vector pixelType +// typedef itk::PointSet PointSetType; +// PointSetType::Pointer pointSet=PointSetType::New(); +// typedef PointSetType::PointType PointType; + +// unsigned int i=0; +// unsigned int pointIndex=0; +// while (i< size()) +// { +// if(m_Data[i]!=-1) +// { +// PointType p; +// p[0]= i;//JV spacing is allways 1 +// pointSet->SetPoint( pointIndex, p ); +// pointSet->SetPointData( pointIndex, m_Data[i] ); +// pointIndex++; +// } +// i++; +// } +// ======= // //--------------------------------------------------------------------- // Signal Signal::ApproximateScatteredValuesWithBSplines(unsigned int splineOrder, unsigned int numberOfControlPoints) // { @@ -899,18 +948,202 @@ namespace clitk { //--------------------------------------------------------------------- + //--------------------------------------------------------------------- + void Signal::ResampleWithTimeWarpTo(clitk::Signal & output, bool lin) const { + double sp = output.GetSamplingPeriod()/output.GetTotalTimeDuration()*GetTotalTimeDuration(); + // DD(GetTotalTimeDuration()); + // DD(output.GetTotalTimeDuration()); + // DD(sp); + if (lin) { + for(uint i=0; i=(int)size())) { + FATAL("out of bound for time " << t << "in this signal having " + << size() << " values with sampling " << GetSamplingPeriod() << std::endl); + } + return (*this)[index]; + } + //--------------------------------------------------------------------- + //--------------------------------------------------------------------- + double Signal::GetValueAtLin(double t) const { + double ti = t/GetSamplingPeriod(); + // DD(t); +// DD(ti); + int index = (int)floor(ti); // floor point + // DD(index); + if (index<0) { + FATAL("out of bound for time " << t << " in this signal having " + << size() << " values with sampling " << GetSamplingPeriod() << std::endl); + } + if (index>=(int)size()-1) { // last value + return (*this)[index]; + } + // DD(index); +// DD(ti-index); +// DD((*this)[index]); +// DD((*this)[index+1]); + return ((ti-index)*((*this)[index+1]) + (1.0-(ti-index))*((*this)[index])); + } + //--------------------------------------------------------------------- + //--------------------------------------------------------------------- + void clitk::Signal::Shift(int d, int length) { + std::cout << "Shift d=" << d << " length = " << length << std::endl; + // int d = (int)lrint(s/GetSamplingPeriod()); // closest integer delta + // DD(d); + clitk::Signal temp; + temp.resize(length);//(uint)size()); + temp.SetSamplingPeriod(GetSamplingPeriod()); + for(uint i=0; iGetGlobalMean(); + mean2=input2.GetGlobalMean(); + + for (unsigned int k= 0;kGetGlobalMean(); + mean2=input2.GetGlobalMean(); + double value=0.; + for (unsigned int n=0;nUnormalizedCorrelation(input2); + cc11=this->UnormalizedCorrelation(input1); + cc22=input2.UnormalizedCorrelation (input2); + cc=cc/sqrt(cc11*cc22); + std::cout<<"coefficient de corrélation : "<UnormalizedXcov(input2,output); + cc11=this->UnormalizedCorrelation(input1); + cc22=input2.UnormalizedCorrelation (input2); + + unsigned int tailleOutput; + tailleOutput=output.size(); + for (unsigned int k= 0;kcoef){ + coef=output[k]; + shift=k; + } + } + cc=this->Correlation(input2); + shift=shift-N+1; + std::cout<<"shift: "<