def plot_wavelet(self): d_obs = self.d_obs d_mod = self.d_mod d_t_obs = self.d_t_obs """ plot data wavelet """ scores = [] for j, site in enumerate(self.sitename): print('Process on Wavelet_' + ''.join(site) + '_No.' + str(j) + '!') fig3 = plt.figure(figsize=(8, 8)) # ax3 = fig6.add_subplot(1, 2, 1) # fig3, ax3 = plt.subplots() data = d_obs[j, :].compressed() time_data = d_t_obs[~d_obs[j, :].mask] # time_data = d_t_obs result = waipy.cwt(data, 1, 1, 0.125, 2, 4 / 0.125, 0.72, 6, mother='Morlet', name='Data') waipy.wavelet_plot('Data', time_data, data, 0.03125, result, fig3) fig3.savefig(self.filedir + self.variable + '/' + ''.join(site) + '_Wavelet_' + self.variable + '.png') for m in range(len(d_mod)): fig4 = plt.figure(figsize=(8, 8)) data = d_mod[m][j, :][~d_obs[j, :].mask] - d_obs[ j, :].compressed() # time_data = d_mod[m][j, :] result = waipy.cwt(data, 1, 1, 0.125, 2, 4 / 0.125, 0.72, 6, mother='Morlet', name='Data') waipy.wavelet_plot('Data', time_data, data, 0.03125, result, fig4) fig4.savefig(self.filedir + self.variable + '/' + ''.join(site) + 'model' + str(m) + '_wavelet_' + self.variable + '.png') plt.close('all') return scores
def main(): ''' d = np.genfromtxt(INFILE).T time, data = d ''' fs, data = wavfile.read(INFILE) plt.plot(data) # plt.show() data = data[10000:11000] # data = scipy.signal.resample(data, len(data)//4) # time = np.arange(0, len(data), 1./fs) time = np.arange(len(data)) data = waipy.normalize(data) result = waipy.cwt(data, 1, 1, 0.25, 2, 4 / 0.25, alpha, 6, mother='Morlet') waipy.wavelet_plot() wavelet(time, data, True) raw_input('')
def main(): n_samples = 1024 # Ruido vermelho S3 = powernoise(2, n_samples) # Caos, usado para gerar o sinal S7 rho = 3.85 a0 = 0.001 S4 = logistic(rho, a0, n_samples) # Soma os sinais e normalizae modo que <A>=0 e std=1 S7 = pre.standardize(S3 + S4) # Sinal gerado pelo pmodel S8 = pmodel(noValues=n_samples, p=0.52, slope=-1.66) data = S8 z = np.linspace(0, 1024, 1024) data_norm = waipy.normalize(data) result = waipy.cwt(data_norm, 1, 1, 0.125, 2, 4 / 0.125, 0.72, 6, mother='h', name='S8') waipy.wavelet_plot('S8', z, data_norm, 0.03125, result)
def Plot_Wavelet(obs, mod, site_id): print('Process on Wavelet ' + 'No.' + str(site_id) + '!') time_data = mod.time data = (obs.extractDatasites(lat=obs.lat, lon=obs.lat)).data[:, site_id] y = (mod.extractDatasites(lat=obs.lat, lon=obs.lat)).data[:, site_id] fig3 = plt.figure(figsize=(8, 8)) result = waipy.cwt(data, 1, 1, 0.125, 2, 4 / 0.125, 0.72, 6, mother='Morlet', name='Obs') ax1, ax2, ax3, ax5 = waipy.wavelet_plot('Obs', time_data, data, 0.03125, result, fig3, unit=obs.unit) change_x_tick(mod, site_id, ax2)
def plot_spectrum_score(self): import waipy, math import numpy as np import matplotlib.pyplot as plt d_obs = self.d_obs d_mod = self.d_mod d_t_obs = self.d_t_obs scores = [] """ Plot global wavelet spectrum """ for j, site in enumerate(self.sitename): if self.sitename.mask[j]: continue print('Process on Spectrum_' + site + '_No.' + str(j) + '!') data = d_obs[j, :].compressed() result = waipy.cwt(data, 1, 1, 0.125, 2, 4 / 0.125, 0.72, 6, mother='Morlet', name='Data') loc_o, scale_o = norm.fit(result['global_ws']) model_score = [] for m in range(len(d_mod)): data = d_mod[m][j, :][~d_obs[j, :].mask] result_temp = waipy.cwt(data, 1, 1, 0.125, 2, 4 / 0.125, 0.72, 6, mother='Morlet', name='Data') loc_m, scale_m = norm.fit(result_temp['global_ws']) model_score.append(abs(loc_m - loc_o)) model_score = model_score / max(model_score) scores.append(model_score) return scores
from scipy import signal import numpy as np import matplotlib.pyplot as plt import waipy datasets = (['data1.dat', 'SAR', 1800], ['data2.dat', 'BIK', 1864], ['data3.dat', 'VUS', 1609]) dt = 1 units = 'mm' for d, name, t0 in datasets: data = np.loadtxt(d) N = data.size time = np.arange(0, N) * dt + t0 label = 'Precipitation data {}'.format(name) data_norm = waipy.normalize(data) alpha = abs(np.corrcoef(data_norm[0:-1], data_norm[1:])[0, 1]) result = waipy.cwt(data_norm, 1, 1, 0.25, 2, 7 / 0.25, alpha, 6, mother='Morlet', name=name) waipy.wavelet_plot(label, time, data_norm, 1.0e-6, result)
def plot_spectrum(self): import waipy, math import numpy as np import matplotlib.pyplot as plt d_obs = self.d_obs d_mod = self.d_mod d_t_obs = self.d_t_obs scores = [] """ Plot global wavelet spectrum """ # col = ['palevioletred', 'm', 'plum', 'darkorchid', 'blue', 'navy', 'deepskyblue', 'darkcyan', 'seagreen', # 'darkgreen', 'olivedrab', 'gold', 'tan', 'red'] for j, site in enumerate(self.sitename): if self.sitename.mask[j]: continue print('Process on Spectrum_' + site + '_No.' + str(j) + '!') data = d_obs[j, :].compressed() if len(data) > 0: result = waipy.cwt(data, 1, 1, 0.125, 2, 4 / 0.125, 0.72, 6, mother='Morlet', name='Data') loc_o, scale_o = norm.fit(result['global_ws']) fig4 = plt.figure(figsize=(4, 4)) ax4 = fig4.add_subplot(1, 1, 1) # f1, sxx1 = waipy.fft(data) # ax.plot(np.log2(1 / f1 * result['dt']), sxx1, 'red', label='Fourier spectrum') # plt.suptitle(self.variable + ' ( ' + self.d_unit_obs + ' )', fontsize=8) ax4.plot(np.log2(result['period']), result['global_ws'], 'k-', label='Wavelet spectrum') ax4.plot(np.log2(result['period']), result['global_signif'], 'r--', label='95% confidence spectrum') model_score = [] for m in range(len(d_mod)): data2 = d_mod[m][j, :][~d_obs[j, :].mask] data = data2.compressed() # data = d_mod[m][j, :][~d_obs[j, :].mask] if len(data) > 0: result_temp = waipy.cwt(data, 1, 1, 0.125, 2, 4 / 0.125, 0.72, 6, mother='Morlet', name='Data') loc_m, scale_m = norm.fit(result_temp['global_ws']) model_score.append(abs(loc_m - loc_o)) ax4.plot(np.log2(result_temp['period']), result_temp['global_ws'], label='Model' + str(m), c=col[m]) else: model_score.append(0.5) model_score = [i / max(model_score) for i in model_score] scores.append(model_score) ax4.legend(bbox_to_anchor=(1.05, 1), loc=2, fontsize=lengendfontsize) # ax4.set_ylim(0, 1.25 * np.max(result['global_ws'])) ax4.set_ylabel('Power', fontsize=fontsize) ax4.set_title('Global Wavelet Spectrum', fontsize=fontsize) y_min = int( min(np.log2(result['period'][0]), np.log2(result_temp['period'][0]))) y_max = int( max( np.log2(result['period'][-1]) + 1, np.log2(result_temp['period'][-1]) + 1)) yt = range(y_min, y_max, 3) # create the vector of period Yticks = [float(math.pow(2, p)) for p in yt] # make 2^periods ax4.set_xticks(yt) ax4.set_xticklabels(Yticks) ax4.set_xlim(xmin=(np.log2(np.min(result['period']))), xmax=(np.log2(np.max(result['period'])))) plt.tight_layout() fig4.savefig(self.filedir + self.variable + '/' + site + '_spectrum_' + self.variable + '.png', bbox_inches='tight') plt.close('all') return scores
def plot_wavelet(self): d_obs = self.d_obs d_mod = self.d_mod d_t_obs = self.d_t_obs """ plot data wavelet """ scores = [] for j, site in enumerate(self.sitename): if self.sitename.mask[j]: continue print('Process on Wavelet_' + site + '_No.' + str(j) + '!') data = d_obs[j, :].compressed() fig3 = plt.figure(figsize=(8, 8)) if len(data) > 0: time_data = d_t_obs[~d_obs[j, :].mask] # time_data = d_t_obs result = waipy.cwt(data, 1, 1, 0.125, 2, 4 / 0.125, 0.72, 6, mother='Morlet', name='Obs') waipy.wavelet_plot('Obs', time_data, data, 0.03125, result, fig3, unit=self.d_unit_obs) # plt.tight_layout() for m in range(len(d_mod)): fig4 = plt.figure(figsize=(8, 8)) data2 = d_mod[m][j, :][~d_obs[j, :].mask] data = data2.compressed() - d_obs[ j, :].compressed()[~data2.mask] if len(data) > 0: result = waipy.cwt(data, 1, 1, 0.125, 2, 4 / 0.125, 0.72, 6, mother='Morlet', name='Obs - Mod' + str(m + 1)) waipy.wavelet_plot('Obs - Mod' + str(m + 1), time_data[~data2.mask], data, 0.03125, result, fig4, unit=self.d_unit_obs, m=m) # plt.tight_layout() fig4.savefig(self.filedir + self.variable + '/' + site + 'model' + str(m) + '_wavelet_' + self.variable + '.png', bbox_inches='tight') fig3.savefig(self.filedir + self.variable + '/' + site + '_Wavelet_' + self.variable + '.png', bbox_inches='tight') plt.close('all') return scores
def plot_spectrum(self): import waipy, math import numpy as np import matplotlib.pyplot as plt d_obs = self.d_obs d_mod = self.d_mod d_t_obs = self.d_t_obs scores = [] """ Plot global wavelet spectrum """ for j, site in enumerate(self.sitename): print('Process on Spectrum_' + ''.join(site) + '_No.' + str(j) + '!') data = d_obs[j, :].compressed() result = waipy.cwt(data, 1, 1, 0.125, 2, 4 / 0.125, 0.72, 6, mother='Morlet', name='Data') fig4 = plt.figure(figsize=(8, 8)) ax4 = fig4.add_subplot(1, 1, 1) # f1, sxx1 = waipy.fft(data) # ax.plot(np.log2(1 / f1 * result['dt']), sxx1, 'red', label='Fourier spectrum') # plt.suptitle(self.variable + ' ( ' + self.d_unit_obs + ' )', fontsize=8) ax4.plot(np.log2(result['period']), result['global_ws'], 'k-', label='Wavelet spectrum') ax4.plot(np.log2(result['period']), result['global_signif'], 'r--', label='95% confidence spectrum') for m in range(len(d_mod)): data = d_mod[m][j, :][~d_obs[j, :].mask] result_temp = waipy.cwt(data, 1, 1, 0.125, 2, 4 / 0.125, 0.72, 6, mother='Morlet', name='Data') ax4.plot(np.log2(result_temp['period']), result_temp['global_ws'], label='Model' + str(m)) ax4.legend(loc=0) ax4.set_ylim(0, 1.25 * np.max(result['global_ws'])) ax4.set_ylabel('Power', fontsize=12) ax4.set_title('Global Wavelet Spectrum', fontsize=12) yt = range(int(np.log2(result['period'][0])), int(np.log2(result['period'][-1]) + 1)) # create the vector of periods Yticks = [float(math.pow(2, p)) for p in yt] # make 2^periods ax4.set_xticks(yt) ax4.set_xticklabels(Yticks) ax4.set_xlim(xmin=(np.log2(np.min(result['period']))), xmax=(np.log2(np.max(result['period'])))) plt.tight_layout() fig4.savefig(self.filedir + self.variable + '/' + ''.join(site) + '_spectrum_' + self.variable + '.png') plt.close('all') return scores
import waipy import numpy as np z = np.linspace(0,2048,2048) x = np.sin(50*np.pi*z) y = np.cos(50*np.pi*z) data_norm = waipy.normalize(x) result = waipy.cwt(data_norm, 1, 1, 0.125, 2, 4/0.125, 0.72, 6, mother='Morlet',name='x') waipy.wavelet_plot('Sine', z, data_norm, 0.03125, result) data_norm1 = waipy.normalize(y) result1 = waipy.cwt(data_norm1, 1, 1, 0.125, 2, 4/0.125, 0.72, 6, mother='Morlet',name='y') waipy.wavelet_plot('Cosine', z, data_norm1, 0.03125, result1) cross_power, coherence, phase_angle = waipy.cross_wavelet(result['wave'], result1['wave']) waipy.plot_cross('Crosspower sine and cosine', cross_power, phase_angle, z, result, result1)
############################################################################## ################################### MAIN ##################################### ############################################################################## namefile = "daily-cases-covid-19.csv" l = pd.read_csv(namefile) codes = list(set(l["Entity"])) codes = codes[1:] l = l.set_index("Entity") values = [] countries = ["Brazil", "India", "Iran", "South Africa", "Egypt"] for i in codes: y = list(l.filter(like=i, axis=0)["Daily confirmed cases (cases)"]) if i in countries: result = waipy.cwt(y, 1, 1, 0.125, 2, 4 / 0.125, 0.72, 6, 'DOG', "x") waipy.wavelet_plot(i, range(len(y)), y, 0.03125, result, savefig=True) if len(y) > 50: alfa, xdfa, ydfa, reta = funcs.dfa1d(y, 1) freqs, power, xdata, ydata, amp, index, powerlaw, INICIO, FIM = funcs.psd( y) values.append([ funcs.variance(y), funcs.skewness(y), funcs.kurtosis(y), alfa, index, mfdfa.makemfdfa(y), i ]) skew2 = [] alfa = [] kurt = []
from waipy import wavelet_plot tick_spacing = 4 data = np.genfromtxt("data/batch_4.txt", delimiter=",")[20000:30000] time = (data[:, 0])#/1000 x = 9.8*(data[:, 1])/2048 y = 9.8*(data[:, 2])/2048 z = 9.8*((data[:, 3]))/2048 # -- factory offset of this particular accelerometer! var = z dt = 5 data_norm = waipy.normalize(var) alpha = np.corrcoef(data_norm[0:-1], data_norm[1:])[0,1]; result = waipy.cwt(data_norm, dt, 0, 0.0625, 1, 36, alpha, 6, mother='Morlet', name="name") waipy.wavelet_plot("img/batch_4_z_start_osc", time, data_norm, 0.03125, result); """ CONTINUOUS WAVELET TRANSFORM pad = 1 # pad the time series with zeroes (recommended) dj = 0.25 # this will do 4 sub-octaves per octave s0 = 2*dt # this says start at a scale of 6 months j1 = 7/dj # this says do 7 powers-of-two with dj sub-octaves each lag1 = 0.72 # lag-1 autocorrelation for red noise background param = 6 mother = 'Morlet' """