<s> """<STR_LIT>""" <EOL> import numpy as np <EOL> import nitime . algorithms as nt_alg <EOL> import nitime . utils as nt_ut <EOL> import matplotlib . pyplot as pp <EOL> """<STR_LIT>""" <EOL> N = <NUM_LIT> <EOL> fft_pow = int ( np . ceil ( np . log2 ( N ) + <NUM_LIT:2> ) ) <EOL> NW = <NUM_LIT:4> <EOL> lines = np . sort ( np . random . randint ( <NUM_LIT:100> , <NUM_LIT:2> ** ( fft_pow - <NUM_LIT:6> ) , size = ( <NUM_LIT:3> , ) ) ) <EOL> while np . any ( np . diff ( lines ) < <NUM_LIT:2> * NW ) : <EOL> lines = np . sort ( np . random . randint ( <NUM_LIT:2> ** ( fft_pow - <NUM_LIT:6> ) , size = ( <NUM_LIT:3> , ) ) ) <EOL> lines = lines . astype ( '<STR_LIT:d>' ) <EOL> """<STR_LIT>""" <EOL> lines += np . random . randn ( <NUM_LIT:3> ) <EOL> """<STR_LIT>""" <EOL> lines /= <NUM_LIT> ** ( fft_pow - <NUM_LIT:2> ) <EOL> phs = np . random . rand ( <NUM_LIT:3> ) * <NUM_LIT:2> * np . pi <EOL> amps = np . sqrt ( <NUM_LIT:2> ) / <NUM_LIT:2> + np . abs ( np . random . randn ( <NUM_LIT:3> ) ) <EOL> """<STR_LIT>""" <EOL> nz_sig = <NUM_LIT:1> <EOL> tx = np . arange ( N ) <EOL> harmonics = amps [ : , None ] * np . cos ( <NUM_LIT:2> * np . pi * tx * lines [ : , None ] + phs [ : , None ] ) <EOL> harmonic = np . sum ( harmonics , axis = <NUM_LIT:0> ) <EOL> nz = np . random . randn ( N ) * nz_sig <EOL> sig = harmonic + nz <EOL> """<STR_LIT>""" <EOL> pp . figure ( ) <EOL> pp . subplot ( <NUM_LIT> ) <EOL> pp . plot ( harmonics . T ) <EOL> pp . xlim ( * ( np . array ( [ <NUM_LIT> , <NUM_LIT> ] ) * N ) . astype ( '<STR_LIT:i>' ) ) <EOL> pp . title ( '<STR_LIT>' ) <EOL> pp . subplot ( <NUM_LIT> ) <EOL> pp . plot ( harmonic , color = '<STR_LIT:k>' , linewidth = <NUM_LIT:3> ) <EOL> pp . plot ( sig , color = ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) , linewidth = <NUM_LIT:2> , linestyle = '<STR_LIT>' ) <EOL> pp . xlim ( * ( np . array ( [ <NUM_LIT> , <NUM_LIT> ] ) * N ) . astype ( '<STR_LIT:i>' ) ) <EOL> pp . title ( '<STR_LIT>' ) <EOL> pp . gcf ( ) . tight_layout ( ) <EOL> """<STR_LIT>""" <EOL> """<STR_LIT>""" <EOL> f , b = nt_ut . detect_lines ( sig , ( NW , <NUM_LIT:2> * NW ) , low_bias = True , NFFT = <NUM_LIT:2> ** fft_pow ) <EOL> h_est = <NUM_LIT:2> * ( b [ : , None ] * np . exp ( <NUM_LIT> * np . pi * tx * f [ : , None ] ) ) . real <EOL> pp . figure ( ) <EOL> pp . subplot ( <NUM_LIT> ) <EOL> pp . plot ( harmonics . T , '<STR_LIT:c>' , linewidth = <NUM_LIT:3> ) <EOL> pp . plot ( h_est . T , '<STR_LIT>' , linewidth = <NUM_LIT:2> ) <EOL> pp . title ( '<STR_LIT>' % h_est . shape [ <NUM_LIT:0> ] ) <EOL> pp . xlim ( * ( np . array ( [ <NUM_LIT> , <NUM_LIT> ] ) * N ) . astype ( '<STR_LIT:i>' ) ) <EOL> pp . subplot ( <NUM_LIT> ) <EOL> err = harmonic - np . sum ( h_est , axis = <NUM_LIT:0> ) <EOL> pp . plot ( err ** <NUM_LIT:2> ) <EOL> pp . title ( '<STR_LIT>' ) <EOL> pp . show ( ) <EOL> """<STR_LIT>""" <EOL> phs_est = np . angle ( b ) <EOL> phs_est [ phs_est < <NUM_LIT:0> ] += <NUM_LIT:2> * np . pi <EOL> phs_err = np . linalg . norm ( phs_est - phs ) ** <NUM_LIT:2> <EOL> amp_err = np . linalg . norm ( amps - <NUM_LIT:2> * np . abs ( b ) ) ** <NUM_LIT:2> / np . linalg . norm ( amps ) ** <NUM_LIT:2> <EOL> freq_err = np . linalg . norm ( lines - f ) ** <NUM_LIT:2> <EOL> print ( '<STR_LIT>' , lines , '<STR_LIT>' , f , '<STR_LIT>' % freq_err ) <EOL> print ( '<STR_LIT>' , amps , '<STR_LIT>' , <NUM_LIT:2> * np . abs ( b ) , '<STR_LIT>' % amp_err ) <EOL> print ( '<STR_LIT>' , phs , '<STR_LIT>' , phs_est , '<STR_LIT>' % phs_err ) <EOL> print ( '<STR_LIT>' % ( np . mean ( err ** <NUM_LIT:2> ) / nz_sig ** <NUM_LIT:2> , ) ) </s>
