0% found this document useful (0 votes)
23 views8 pages

Signal Processing with Wavelet Transform

The document contains Python code for signal processing, including functions to normalize signals, generate Ricker and Morlet wavelets, and compute dominant frequencies using the Hilbert transform. It also implements a discrete wavelet transform (DWT) and visualizes the results using Matplotlib. Various test parameters and configurations are provided for generating and analyzing signals.

Uploaded by

Danielle C Costa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views8 pages

Signal Processing with Wavelet Transform

The document contains Python code for signal processing, including functions to normalize signals, generate Ricker and Morlet wavelets, and compute dominant frequencies using the Hilbert transform. It also implements a discrete wavelet transform (DWT) and visualizes the results using Matplotlib. Various test parameters and configurations are provided for generating and analyzing signals.

Uploaded by

Danielle C Costa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

In [1]: import numpy as np

import [Link] as plt


from [Link] import hilbert
from scipy import signal

In [2]: def normalize(x):


x_norm = [Link]()
if [Link](x)<0:
x_norm = x_norm - [Link](x_norm)

x_max, x_min = [Link](x_norm), [Link](x_norm)

for i in range(len(x_norm)):
x_norm[i] = (x_norm[i] - x_min)/(x_max-x_min)

return x_norm

In [3]: def ricker_wav(t, t0, f):


"""
Ricker wavelet function with normalized energy.
t: time array
f: dominant frequency
"""
return 2*[Link](f*[Link](2*[Link])/3)*(1-2*([Link]*f*(t-t0))**2)*[Link](

s(t) = exp (−c(2πft)2/√1 − c2) cos 2πft (1)

In [4]: def morlet_wav(t, c, f_c):


return [Link](-c*(2*[Link]*f_c*t)**2/[Link](1 - c**2))*[Link](2*[Link]*f_

In [5]: #t = [Link](-0.05, 0.05, 200) #não rola com esses valores


#t = [Link](-2, 2, 500) #outros intervalos para testes
#t = [Link](5, 0, 500) #outros intervalos para testes (não rola com ess
t = [Link](0, 5, 500) #outros intervalos para testes
#t = [Link](-4, 4, 1000) #outros intervalos para testes

#s = [Link](2*[Link]*50*t)
#s = 0.7* [Link] (7*t)+1.5*[Link](3.47*t) #soma de dois senos aleaórios

#sin1 = [Link](3*t)
#sin2 = [Link](6*t)
#s = (sin1, sin2) # junção em (0,0) de dois senos de freq. diferentes

#s = [Link](3*t) # Gabor aleatorio

#s = (1-t*t)*[Link](-t*t/2) # Ricker
s = [Link]((t+5)*(1+1.5*(t+5))) # Chirp para comecar em t=-2

#s = ricker_wav(t, 0.0, 50)


#s = morlet_wav(t, 0.1, 50)
s = [Link](s, dtype=np.float32) #Pq redefinir o s?

dt = t[1] - t[0] #Dúvida nesses valores!

[Link](t, s )
[Link]('t')
[Link]('s(t)')
[Link]()

In [6]: #valores de testes


fmin = 1
fmax = 7
fn = 100

#fmin = 1
#fmax = 100
#fn = 300

#fmin = 10
#fmax = 100
#fn = 300

#valores originais
#fmin = 0.1
#fmax = 100
#fn = 300

f = [Link](fmin, fmax, fn)


In [7]: def dom_freq(s, t, r, sigma):

"""Compute the dominant frequency by weight averaging the instantaneous


using a Gaussian low-pass filter.

Parameters
----------
s : array
the time signal
t : array
the time components of signal s
r: float
the scaling factor
sigma: float
the spread parameter for the Gaussian filter
"""

dt = t[1]-t[0] # timestep

a_s = hilbert(s)

x = [Link](a_s)
dx = [Link](x)

y = [Link](a_s)
dy = [Link](y)

a = [Link](a_s)

f0 = (-(dx/dt)*y + (dy/dt)*x)/(2*[Link]*a**2) # instantaneous frequency

L = [Link](len(s), std=sigma)

dom_freq = [Link](f0*a, L, mode='same')

const = [Link](a, L, mode='same')

return r*dom_freq/const

r ∫ finst (t − τ)a(t − τ)L(t; σ) dt


fdom (τ; r, σ) = (2)
∫ a(t − τ)L(t; σ) dt

In [8]: r = 1.0
#sigma = 10
#sigma = 50
#sigma = 75
sigma = 100
#sigma = 20 # 20 pontos para fazer a janela
f0 = dom_freq(s, t, r, sigma)

[Link](t, f0)

[Link](fmin, fmax)
[Link]('Time (s)')
[Link]('Frequency (Hz)')

[Link]()

In [9]: def DWT(x, tau, f, f0, k1, k2, p):


"""
Function to calculate the
discrete W Transform of a 1D real-valued signal xN

x: signal
"""
t = [Link]()

f = [Link](f, (len(f), 1))

# WT(t, f) a matrix Nt x Nf
X = [Link]((len(f), len(t)), dtype=complex)

f0_interp = [Link]()

for i in range(len(t)):
sigma = 1/((f0_interp[i]/k1)**p + ([Link](f0_interp[i] - f)/k2)**p)*

a = [Link](-(tau-t[i])**2/(2*sigma**2))/(sigma*[Link](2*[Link]))

e = [Link](-2j*[Link]*f*tau)*a
X[:, i] = [Link](e, x)

return X, t, f

In [10]: #Valores de teste


#k1 = 0.5
#k2 =1
#p = 5.0

#k1 = 1.0
#k2 = 0.5
#p = 5.0

#k1 = 1.5
#k2 = 1.0
#p = 5.0

#k1 = 1.0
#k2 = 2.0
#p = 5.0

k1 = 1.0
k2 = 2.5
p = 5.0

#k1 = 1.0
#k2 = 1.5
#p = 5.0

#Valor original
#k1 = 1.0
#k2 = 0.9
#p = 5.0

WT, _, _ = DWT(s, t, f, f0, k1, k2, p)


ST, _, _ = DWT(s, t, f, [Link](len(f0)), 1.0, 1.0, 1.0)

#Valores de testes
#ST, _, _ = DWT(s, t, f, [Link](len(f0)), 0.5, 0.5, 0.5)
#ST, _, _ = DWT(s, t, f, [Link](len(f0)), 2.0, 2.0, 2.0)
#ST, _, _ = DWT(s, t, f, [Link](len(f0)), 5.0, 5.0, 5.0)

In [11]: import [Link] as ticker


from [Link] import NullFormatter
from mpl_toolkits.axes_grid1 import make_axes_locatable

fig = [Link](constrained_layout=True)

gs = fig.add_gridspec(3, 1)

################################################
f_ax1 = fig.add_subplot(gs[0, 0])

[Link](t, s, color='red', ls='dotted', linewidth=0.5)


f_ax1.set_xticks([])
[Link](t[0], t[-1])
[Link]([Link](s), [Link](s))

[Link](r'Amplitude', size= 10)

################################################
f_ax1 = fig.add_subplot(gs[1, 0])

im1= [Link](t, f, normalize([Link](ST)), cmap='jet')


#[Link](t, f0, color='white', ls='--', linewidth=0.5)
#[Link](orientation='horizontal', label='Power Index', pad=0.1)
[Link](t, f0, color='white', ls='--', linewidth=0.9)

[Link](fmin, fmax)
[Link](t[0], t[-1])

[Link](fontsize = 10)

[Link]('Frequency (Hz)', size= 10)

[Link]("ST", loc='left')

################################################
f_ax2 = fig.add_subplot(gs[2, 0])

im2 = [Link](t, f, normalize([Link](WT)), cmap='jet')


[Link](t, f0, color='white', ls='--', linewidth=0.9)

[Link](fmin, fmax)
[Link](t[0], t[-1])

[Link](fontsize = 10)

[Link]("WT", loc='left')

[Link]('Frequency (Hz)', size= 10)


[Link]('Time (s)', size=10)

[Link](orientation='horizontal', label='Normalized Amplitude', pad=0.1

fig.set_size_inches(3, 5)
fig.set_dpi(300)
[Link]("[Link]", bbox_inches='tight')

[Link]()
In [12]: #print([Link](len(f0)))

You might also like