Диссертация (1105240), страница 18
Текст из файла (страница 18)
w r i t e ( r e i n t e r p r e t _ c a s t <c o n s t ch ar *>( a r r a y ) , a r r a y _ s i z e *94s i z e o f (T) ) ;current_matrix_data_count+= a r r a y _ s i z e ;9596}97t e m p l a t e <typename T> v o i d QMatFile : : f i n i s h M a t r i x ( )98{99uint32_t read_bytes_count = current_matrix_data_count * s i z e o f (T) ;100i f ( read_bytes_count%8)101{102i n t needed = 8 - ( read_bytes_count%8) ;103char padding [ needed ] ;104memset ( padding , 0 , needed ) ;105ostm .
w r i t e ( padding , needed ) ;106read_bytes_count+=needed ;107}108109ostm . s e e k p ( current_matrix_size_pos ) ;110uint32_t m a t r i x _ s i z e = ArrayFlags : : k A r r a y F l a g s F u l l S i z e B y t e s+111kMatrixArrayDimSizeBytes+112nameElementSize ( )+113(8+ read_bytes_count ) ;114write_value_native <uint32_t >(&m a t r i x _ s i z e , ostm ) ;122115116ostm . s e e k p ( current_matrix_size_pos +28) ; // f i r s t dimension ;117write_value_native <uint32_t >(¤t_matrix_data_count , ostm ) ;118ostm .
s e e k p ( current_matrix_size_pos+nameElementSize ( ) +40) ; // a r r a y119s i z e in bytes120read_bytes_count = current_matrix_data_count * s i z e o f (T) ;121write_value_native <uint32_t >(&read_bytes_count , ostm ) ;122ostm . s e e k p ( 0 , s t d : : i o s _ b a s e : : end ) ;123}124125t e m p l a t e <typename T> v o i d QMatFile : : w r i t e M a t r i x ( MatrixDataClassc l a s s _ t y p e , c o n s t QString& name , DataTypeTag data_type ,c o n s t T* array , uint32_t126array_size )127{128s t a r t M a t r i x ( c l a s s _ t y p e , name , data_type ) ;129appendDataToMatrix<T>( array , a r r a y _ s i z e ) ;130f i n i s h M a t r i x <T>() ;131}А.3.
Реализация выделения амплитуды и фазы сигнала назаданной частоте методом гетеродинирования наязыке С++1b o o l DSPHeterodyne : : heterodyneAmplitude ( HeterodyneParams& params ,c o n s t PRMConverter& c o n v e r t e r )2{3uint32_t c h u n k S i z e = 5* params . FS ;4SmartArray<double> normdArr ( c h u n k S i z e ) , p r o c e s s e d A r r ( params .f i l t e r _ s i g n a l ? chunkSize : 0) ,56sinusArr ( chunkSize ) , cosinusArr ( chunkSize ) ;d ouble * normd = normdArr .
p o i n t e r ( ) ;1237d ouble * p r o c e s s e d = params . f i l t e r _ s i g n a l ? p r o c e s s e d A r r . p o i n t e r ( ): normd ;8d ouble * s i n u s = s i n u s A r r . p o i n t e r ( ) ;9d ouble * c o s i n u s = c o s i n u s A r r . p o i n t e r ( ) ;1011d ouble f i r s t _ f i l t e r _ g a i n , s e c o n d _ f i l t e r _ g a i n ;12QDSPHelper : : D S P F i l t e r s H e l p e r m_helper , m_sin_helper , m_cos_helper ;13QFile o u t p u t F i l e ;14QTextStream ostm ;15size_t csamples ;1617QHDDTmpFileWriter<double> * amplTmpFile = 0 , * phaseTmpFile = 0 ;18QHDDTmpFileWriter<double> * f f i l t T m p F i l e = 0 , * sinTmpFile = 0 , *cosTmpFile = 0 ;19d ouble ampl , phase ;20d ouble ampl_mult ;2122QHDDTmpFileReader<double> normdFileReader ( params .
tmpFile , f a l s e ) ;2324s e t u p B a n d P a s s F i l t e r ( params . f0 , params . df , params . FS , m_helper ) ;25f i r s t _ f i l t e r _ g a i n = params . f i l t e r _ s i g n a l ? m_helper . magnitude (params . f 0 ) : 1 ;26qLHDebug<<" Legendre ␣ f i l t e r ␣ f 0 : "<<params . f0 <<" , ␣bw : "<<params . df<<"␣r e s _ g a i n : "<<f i r s t _ f i l t e r _ g a i n ;2728// //29i f ( ! s e t u p L o w P a s s F i l t e r ( params .
f0 , params . FS , m_sin_helper ) | |! s e t u p L o w P a s s F i l t e r ( params . f0 , params . FS , m_cos_helper ) )3031{qLHWarning<<" Cannot ␣ s e t u p ␣ Butterworth ␣ BandPass ␣ f i l t e r ␣ - ␣ check ␣32params " ;return f a l s e ;3334}35s e c o n d _ f i l t e r _ g a i n = m_sin_helper . magnitude ( 0 ) ;12436qLHDebug<<" Butterworth ␣ lowPass ␣bw : "<<params . f 0 /*qMin ( params . f 0/ 2 . , 1 . ) */<<" , ␣ 0 - g a i n : "<<s e c o n d _ f i l t e r _ g a i n ;3738i f ( ! params . outputFileName . isEmpty ( ) )39{40o u t p u t F i l e . setFileName ( params . outputFileName ) ;41i f ( ! o u t p u t F i l e .
open ( QFile : : WriteOnly ) )42{qLHWarning<<" Cannot ␣ w r i t e ␣ampl&phase ␣"<<43QSUP_FILEWRITE_ERROR( o u t p u t F i l e ) ;return f a l s e ;4445}46qLHDebug<<" Writing ␣ amplitude ␣&␣ phase ␣ t o ␣"<<params .outputFileName ;4748ostm .
s e t D e v i c e (& o u t p u t F i l e ) ;49i f ( params . i s _ t h e t a )50{ostm<<QString ( "#T␣Theta_%1␣M_%1␣ Phase \n#s ␣ rad ␣Nm␣ rad " ) . ar g51( i n t ( params . f 0 ) ) ;52}53else54{ostm<<QString ( "#T␣Ampl_%1␣ Phase \n#s ␣V␣ rad " ) .
a rg ( i n t ( params55. f0 ) ) ;56}57i f ( ! params . outputFileComments . isEmpty ( ) )58{ostm<<"#"<<params . outputFileComments ;5960}6162ostm<<"\n#FS : "<<params . FS<<"␣ MainFreq : "<<params . f 063<<"␣Fmain : "<<params . fmain64<<"\n#S t a r t e d : ␣"<<QMat : : toMatlabDateTime ( params .blockStartTime ) ;1256566ostm . setRealNumberNotation ( QTextStream : : S c i e n t i f i c N o t a t i o n ) ;67ostm . s e t R e a l N u m b e r P r e c i s i o n ( 8 ) ;68}6970i f ( params .
save_ampl_phase )71{72params . tmp_ampl = QHDDTmpFile : : mktmp( "hampl" ) ;73amplTmpFile = new QHDDTmpFileWriter<double >(params . tmp_ampl ) ;74i f ( ! amplTmpFile -> i s V a l i d ( ) )75{76d e l e t e amplTmpFile ;77amplTmpFile = 0 ;78params . tmp_ampl . c l e a r ( ) ;79}80else81{82params . tmp_phase = QHDDTmpFile : : mktmp( " hphase " ) ;83phaseTmpFile = new QHDDTmpFileWriter<double >(params .tmp_phase ) ;84i f ( ! phaseTmpFile -> i s V a l i d ( ) )85{86FREE_POINTER( amplTmpFile ) ;87QFile ( params .
tmp_ampl ) . remove ( ) ;88params . tmp_ampl . c l e a r ( ) ;89FREE_POINTER( phaseTmpFile ) ;90params . tmp_phase . c l e a r ( ) ;}9192}9394params . save_ampl_phase = ( amplTmpFile != 0 ) && ( phaseTmpFile!= 0 ) ;9596i f ( params . save_ampl_phase )97{126qLHDebug<<"Tmp␣ amplitude ␣->␣"<<params . tmp_ampl<<" , ␣ phase ␣98->␣"<<params . tmp_phase ;}99100}101102i f ( params . s a v e _ s i g n a l s )103{104params .
t m p _ f i l t e r e d = QHDDTmpFile : : mktmp( " f i l t e r e d " ) ;105f f i l t T m p F i l e = new QHDDTmpFileWriter<double >(params .tmp_filtered ) ;106i f ( ! f f i l t T m p F i l e -> i s V a l i d ( ) )107{108FREE_POINTER( f f i l t T m p F i l e ) ;109params . t m p _ f i l t e r e d . c l e a r ( ) ;}110111112params . tmp_sin = QHDDTmpFile : : mktmp( " s i n " ) ;113sinTmpFile = new QHDDTmpFileWriter<double >(params . tmp_sin ) ;114i f ( ! sinTmpFile -> i s V a l i d ( ) )115{116FREE_POINTER( sinTmpFile ) ;117params . tmp_sin .
c l e a r ( ) ;}118119120params . tmp_cos = QHDDTmpFile : : mktmp( " c o s " ) ;121cosTmpFile = new QHDDTmpFileWriter<double >(params . tmp_cos ) ;122i f ( ! cosTmpFile -> i s V a l i d ( ) )123{124FREE_POINTER( cosTmpFile ) ;125params . tmp_cos . c l e a r ( ) ;}126127}128129ampl_mult = params . i s _ t h e t a ? PRMSetupConstants : : kNormdToTheta :1.;127130131d ouble s t a r t T i m e = params . s t a r t T i m e ;132PhaseUnwrapper unwrap ;133w h i l e ( normdFileReader .
a v a i l a b l e ( ) >0)134{135c s a m p l e s = normdFileReader . read ( normd , params . FS ) ;136i f ( c s a m p l e s == 0 )137{break ;138139}140141f o r ( uint32_t i =0; i <c s a m p l e s;++ i )142{143s i n u s [ i ] = s i n ( 2*M_PI* params . f 0 * s t a r t T i m e ) ;144c o s i n u s [ i ] = c o s ( 2 *M_PI* params . f 0 * s t a r t T i m e ) ;145s t a r t T i m e+= 1 .
/ params . FS ;146}147148i f ( params . f i l t e r _ s i g n a l )m_helper . p r o c e s s ( normd , p r o c e s s e d , c s a m p l e s ) ;149150151i f ( ffiltTmpFile )152{f f i l t T m p F i l e ->append ( p r o c e s s e d , c s a m p l e s ) ;153154}155156dotProduct ( s i n u s , p r o c e s s e d , c s a m p l e s ) ;157dotProduct ( c o s i n u s , p r o c e s s e d , c s a m p l e s ) ;158m_sin_helper . p r o c e s s ( s i n u s , c s a m p l e s ) ;159m_cos_helper . p r o c e s s ( c o s i n u s , c s a m p l e s ) ;160161i f ( sinTmpFile )162{sinTmpFile ->append ( c o s i n u s , c s a m p l e s ) ;163164}128165i f ( cosTmpFile )166{cosTmpFile ->append ( s i n u s , c s a m p l e s ) ;167168}169170f o r ( i n t k=0;k<c s a m p l e s;++k )171{172ampl = 2* s q r t ( s i n u s [ k ] * s i n u s [ k]+ c o s i n u s [ k ] * c o s i n u s [ k ] ) ;173phase = unwrap .
s t e p ( atan2 ( c o s i n u s [ k ] , s i n u s [ k ] ) ) ;174s i n u s [ k ] = ampl*ampl_mult/ ( f i r s t _ f i l t e r _ g a i n *second_filter_gain ) ;c o s i n u s [ k ] = phase ;175176}177178i f ( amplTmpFile )179{amplTmpFile ->append ( s i n u s , c s a m p l e s ) ;180181}182i f ( phaseTmpFile )183{phaseTmpFile ->append ( c o s i n u s , c s a m p l e s ) ;184185}186i f ( o u t p u t F i l e . isOpen ( ) )187{188i f ( params . i s _ t h e t a )189{190ampl = c a l c u l a t e M e a n S t d d e v ( s i n u s , c s a m p l e s ) . f i r s t ;191ostm<<"\n"<<(startTime - 0 . 5 * c s a m p l e s / params .
FS )<<"␣"<<ampl<<"␣"<<c o n v e r t e r . thetaToM ( ampl , params . f0 , params .192fmain )<<"␣"<<c a l c u l a t e M e a n S t d d e v ( c o s i n u s , c s a m p l e s ) . f i r s t ;193194}195else196{129ostm<<"\n"<<(startTime - 0 . 5 * c s a m p l e s / params . FS )197<<"␣"<<c a l c u l a t e M e a n S t d d e v ( s i n u s , c s a m p l e s ) .