sharpness = 5000         # with ideally dense sampled data, this should converge to infinity; reduce it to avoid ringing 
    return -1j * np.sum(y * np.arctan(1/(new_x_grid - old_x_grid)/sharpness)*sharpness, axis=1) / len(x) / (2*pi)
if test_kramers_kronig:
    ## Test the Kramers-Kronig relations - in f
    new_f = np.linspace(-maxplotf, maxplotf, 1000)
    conv = naive_hilbert_transform(f, yf, new_f)
    #conv = naive_hilbert_transform(freq, yf2, new_f) ## FIXME - using these data, the KK amplitude goes wrong
    plot_complex(new_f, conv, c='b', alpha=.6, lw=.5, label='KKR in $f$') 

## Spectrum 4: use the Filter-Diagonalisation Method, implemented in Harminv, to find discrete oscillators
if harmonic_inversion:
    import harminv_wrapper
    tscale = 1.0     ## harminv output may have to be tuned by changing this value
    x = x[int(len(x)*FDMtrunc[0]):int(len(x)*FDMtrunc[1])]*tscale
    y = y[int(len(y)*FDMtrunc[0]):int(len(y)*FDMtrunc[1])]
    hi = harminv_wrapper.harminv(x, y, amplitude_prescaling=None)
    hi['frequency'] *= tscale 
    hi['decay'] *= tscale

    oscillator_count = len(hi['frequency'])
    freq_fine = np.linspace(-maxplotf, maxplotf, 2000)
    sumosc = np.zeros_like(freq_fine)*1j
    for osc in range(oscillator_count):
        osc_y = lorentz(omega=freq_fine*2*pi,   
                omega0=hi['frequency'][osc]*2*pi, 
                gamma=hi['decay'][osc]*2*pi, 
                ampli=hi['amplitude'][osc] * np.abs(hi['frequency'][osc])*2*pi)
        sumosc += osc_y 
    plot_complex(freq_fine, sumosc, color='g', alpha=.6, lw=2, label=u"Harminv modes sum")      # (optional) plot amplitude
    Wh = np.trapz(y=np.abs(sumosc)**2, x=freq_fine); print 'Plancherel theorem test: Energy in Harminv f               :', Wh, '(i.e. %.5g of timedomain)' % (Wh/Wt)
    #print 'All harminv oscillators (frequency, decay and amplitude):\n', np.vstack([hi['frequency'], hi['decay'], hi['amplitude']])
Example #2
0
        for line in datafile:
            if line[0:1] in "0123456789": break         # end of file header
            value = line.replace(",", " ").split()[-1]  # the value of the parameter will be separated by space or comma
            if not Kz and ("Kz" in line): Kz = float(value)
            if not cellsize and ("cellsize" in line): cellsize = float(value)
    Kz = Kz*cellsize/2/np.pi
    (t, E) = np.loadtxt(filename, usecols=list(range(2)), unpack=True, )

    if plot_FDM:
        import harminv_wrapper
        tscale = 3e9            ## TODO check again that this prescaling is needed
        t1 = t[len(t)*FDMtrunc[0]:len(t)*FDMtrunc[1]]*tscale
        t1 -= np.min(t1)
        E1 = E[len(t)*FDMtrunc[0]:len(t)*FDMtrunc[1]]
        try:
            hi = harminv_wrapper.harminv(t1, E1, d=.1, f=15)
            hi['frequency'] *= tscale /frequnit
            hi['amplitude'] /= np.max(hi['amplitude'])
            hi['error'] /= np.max(hi['amplitude'])
            FDM_freqs = np.append(FDM_freqs,  hi['frequency'])
            FDM_amplis= np.append(FDM_amplis,  hi['amplitude'])
            FDM_phases= np.append(FDM_phases,  hi['phase'])
            FDM_Kzs   = np.append(FDM_Kzs,    Kz*np.ones_like(hi['frequency'])) 
        except: 
            print "Error: Harminv did not find any oscillator in %s" % filename


    if plot_FFT:
        for field in (E,):
            field[t>max(t)*FFTcutoff] = field[t>max(t)*FFTcutoff]*(.5 + .5*np.cos(np.pi * (t[t>max(t)*FFTcutoff]/max(t)-FFTcutoff)/(1-FFTcutoff)))
        ## 1D FFT with cropping for useful frequencies

if test_kramers_kronig:
    ## Test the Kramers-Kronig relations - in f
    new_f = np.linspace(-maxplotf, maxplotf, 1000)
    conv = naive_hilbert_transform(f, yf, new_f)
    #conv = naive_hilbert_transform(freq, yf2, new_f) ## FIXME - using these data, the KK amplitude goes wrong
    plot_complex(new_f, conv, c='b', alpha=.6, lw=.5, label='KKR in $f$')

## Spectrum 4: use the Filter-Diagonalisation Method, implemented in Harminv, to find discrete oscillators
if harmonic_inversion:
    import harminv_wrapper
    tscale = 1.0  ## harminv output may have to be tuned by changing this value
    x = x[int(len(x) * FDMtrunc[0]):int(len(x) * FDMtrunc[1])] * tscale
    y = y[int(len(y) * FDMtrunc[0]):int(len(y) * FDMtrunc[1])]
    hi = harminv_wrapper.harminv(x, y, amplitude_prescaling=None)
    hi['frequency'] *= tscale
    hi['decay'] *= tscale

    oscillator_count = len(hi['frequency'])
    freq_fine = np.linspace(-maxplotf, maxplotf, 2000)
    sumosc = np.zeros_like(freq_fine) * 1j
    for osc in range(oscillator_count):
        osc_y = lorentz(omega=freq_fine * 2 * pi,
                        omega0=hi['frequency'][osc] * 2 * pi,
                        gamma=hi['decay'][osc] * 2 * pi,
                        ampli=hi['amplitude'][osc] *
                        np.abs(hi['frequency'][osc]) * 2 * pi)
        sumosc += osc_y
    plot_complex(freq_fine,
                 sumosc,