Пример #1
0
def cwt_iter(fseq,
             frange,
             nfreqs = 128,
             wavelet = pycwt.Morlet(),
             normL = None,
             max_pixels = None,
	     cwtfn = pycwt.eds,
             verbose = False,
             **kwargs):
    """
    Iterate over cwt of the time series for each pixel

    Parameters:
      - `fseq` -- frame sequence instance
      - `frange`  -- frequency range as a pair or vector of frequencies
      - `nfreqs` -- number of frequencies/scales for decomposition 
      - `wavelet` -- wavelet object [pycwt.Morlet()]
      - `normL` -- length of normalizing part (baseline) of the time series 
      - `max_pixels` -- upper limit on number of pixels to iterate over 
      - `cwt_fn` -- function to process wavelet coefficients [pycwt.eds]
      - `verbose` -- be verbose 
      - `**kwargs` --  are passed to fseq.pix_iter

    Returns:
     - generator over (cwt-derived measure, i, j) tuples
      (where i,j are frame indices)
    """
    tick = time.clock()
    L = len(fseq)
    subframe = kwargs.has_key('sliceobj') and kwargs['sliceobj'] or None
    shape = fseq.shape(subframe)
    npix = shape[0]*shape[1]
    normL = ifnot(normL, L)
    pixel_iter = fseq.pix_iter(**kwargs)
    max_pixels = ifnot(max_pixels, npix)

    if len(frange) == 2:  # a low-high pair
        freqs = np.linspace(frange[0], frange[1], num=nfreqs)
    else:
        freqs= np.array(frange.copy())
    nfreqs = len(freqs)

    pixel_counter = 0
    npix = min(npix, max_pixels)
    cwtf = pycwt.cwt_f
    dt, tunits = fseq.meta['axes'][0]
    for s,i,j in pixel_iter:
	# todo: normalization should be optional or as an argument to pix_iter
        s = (s-np.mean(s[:normL]))/np.std(s[:normL])
        eds = pycwt.eds(cwtf(s, freqs, 1./dt, wavelet, 'zpd'))
        pixel_counter+= 1
        if verbose:
            sys.stderr.write("\rpixel %05d of %05d"%(pixel_counter,npix))
        yield eds, i, j
        if pixel_counter > max_pixels:
            break
    if verbose:
        sys.stderr.write("\n Finished in %3.2f s\n"%(time.clock()-tick))
Пример #2
0
def wavelet(nni=None,
            rpeaks=None,
            fs=4.,
            fbands=None,
            detrend=True,
            show=True,
            show_param=True,
            legend=True):

    # Check input
    nn = tools.check_input(nni, rpeaks)
    # Verify or set default frequency bands
    fbands = fd._check_freq_bands(fbands)

    # 外れ値をスプライン補間
    nni = _artefact_correction(nni, threshold=0.25)

    # 4Hzでリサンプリング
    nn_interpol = detrending.resample_to_4Hz(nni, fs)

    # 平滑化
    if detrend:
        nn_interpol = detrending.detrend(nn_interpol, Lambda=500)

    freqs = np.arange(0.001, 1, 0.025)
    omega0 = 8
    r = pycwt.cwt_f(nn_interpol, freqs, fs, pycwt.Morlet(omega0))
    rr = np.abs(r)
    t_interpol = np.arange(0, len(nn_interpol) / fs, 1. / fs)
    plt.imshow(rr,
               aspect='auto',
               extent=(t_interpol[0], t_interpol[-1], freqs[0], freqs[-1]))
    plt.xlabel('Time[s]')
    plt.ylabel('Frequency[Hz]')
    plt.show()
    pass
Пример #3
0
starttime = float(sys.argv[1])
endtime = float(sys.argv[2])
start = int(starttime * fs)
end = int(endtime * fs)

del starttime, endtime

specdatab = np.array(df[start:end])
specdataa = specdatab.flatten()

fp.close

P = 1024
widths = np.arange(1, P)

cwtmatr = pycwt.cwt_f(specdataa, widths, fs, pycwt.Morlet())
rr = np.abs(cwtmatr)
del cwtmatr

spec = pd.DataFrame(rr)
del specdataa

del rr
s = 100
print(len(df[start:end]))
spec_new = pd.DataFrame(index=spec.index, columns=[])
for i in range(0, len(df[start:end]), s):
    spec1 = spec[i]
    spec_new[i / s] = spec1
spec_new.T.to_csv(sys.argv[3] + '-' + sys.argv[4] + '-' + sys.argv[1] + '-' +
                  sys.argv[2] + '-cut.csv',
Пример #4
0
from swan import pycwt
import numpy as np
import matplotlib.pyplot as plt

x = np.arange(0, 20, 0.01)
y = np.sin(2 * np.pi * 2 * x) * 2 + np.sin(2 * np.pi * 5 * x) * \
    2 + np.sin(2 * np.pi * 10 * x)
plt.plot(x, y)
plt.show()

Fs = 1000
omega0 = 8
# (1) Freqを指定してcwt
freqs = np.arange(0.1, 20, 0.025)
r = pycwt.cwt_f(y, freqs, Fs, pycwt.Morlet(omega0))
rr = np.abs(r)
plt.rcParams['figure.figsize'] = (10, 6)
fig = plt.figure()
ax1 = fig.add_axes([0.1, 0.75, 0.7, 0.2])
ax2 = fig.add_axes([0.1, 0.1, 0.7, 0.60], sharex=ax1)
ax3 = fig.add_axes([0.83, 0.1, 0.03, 0.6])
ax1.plot(x, y, 'k')
# , interpolation='nearest')
img = ax2.imshow(np.flipud(rr), extent=[0, 20, 0.1, 20], aspect='auto')
twin_ax = ax2
twin_ax.set_yscale('log')
twin_ax.set_xlim(0, 20)
twin_ax.set_ylim(0.1, 20)
ax2.tick_params(which='both', labelleft=False, left=False)
twin_ax.tick_params(which='both', labelleft=True, left=True, labelright=False)
fig.colorbar(img, cax=ax3)
Пример #5
0
del datatime

specdatab = np.array(df[start:end])
del df
specdataa = specdatab.flatten()
fp.close
del specdatab
#widths = np.arange(1, 31)
P = 1024
#P = 16384

widths = np.arange(1, P)

plt.subplot(2, 1, 2)
fc = 1.5
cwtmatr = pycwt.cwt_f(specdataa, widths, 25000, pycwt.Morlet(fc))
rr = np.abs(cwtmatr)

del widths
#im = plt.imshow(np.flipud(rr), extent=[starttime, endtime, P, 1], aspect='auto',interpolation='nearest')
im = plt.imshow(np.flipud(rr),
                extent=[0, (endtime - starttime) * 1000, P, 1],
                aspect='auto',
                interpolation='nearest')
#xlim(starttime, endtime)
#xlim(0, (endtime-starttime)*1000)
#ylim(0, 4096)
plt.tick_params(length=10)
xlabel("time [ms]")
ylabel("frequency [Hz]  ")
#plt.yscale("log")