Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
Multi Scale Causality
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
FROGE Ewen
Multi Scale Causality
Commits
fcbe0d45
Commit
fcbe0d45
authored
1 year ago
by
FROGE Ewen
Browse files
Options
Downloads
Patches
Plain Diff
Upload New File
parent
aa16cbd1
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
PyLibrary/EwenLib/Filters.py
+318
-0
318 additions, 0 deletions
PyLibrary/EwenLib/Filters.py
with
318 additions
and
0 deletions
PyLibrary/EwenLib/Filters.py
0 → 100644
+
318
−
0
View file @
fcbe0d45
import
numpy
as
np
import
matplotlib.pyplot
as
plt
from
scipy.signal
import
*
#####
#ALL Filters applies along first dimension if multiple dimensions are given
####
#%%
def
Box
(
signal1
,
tau1
,
verbose
=
False
):
print
(
np
.
size
(
np
.
shape
(
signal1
)))
signal1
=
np
.
squeeze
(
signal1
)
Shape
=
list
(
np
.
shape
(
signal1
))
Result1
=
np
.
zeros
(
signal1
.
shape
)
#Result1[tau1:]=signal1[tau1:]-signal1[:-tau1]
Shape
[
0
]
=
2
*
tau1
+
1
kernel
=
np
.
ones
(
Shape
)
/
(
2
*
tau1
+
1
)
#kernel[tau1]=1
Result1
=
fftconvolve
(
signal1
,
kernel
,
mode
=
'
full
'
,
axes
=
0
)[
tau1
:
-
tau1
]
fp
=
tau1
lp
=
len
(
signal1
)
-
tau1
if
verbose
:
plt
.
figure
(
1
)
plt
.
clf
()
#plt.subplot(121)
plt
.
plot
(
signal1
,
'
k.-
'
)
plt
.
plot
(
Result1
,
'
r-
'
)
return
Result1
,
fp
,
lp
#%%
def
CausalBox
(
signal1
,
tau1
,
verbose
=
False
,
order
=
[]):
#Result1=np.zeros(signal1.shape)
#Result1[tau1:]=signal1[tau1:]-signal1[:-tau1]
signal1
=
np
.
squeeze
(
signal1
)
#Result1[tau1:]=signal1[tau1:]-signal1[:-tau1]
Shape
=
list
(
np
.
shape
(
signal1
))
Shape
[
0
]
=
tau1
+
1
kernel
=
np
.
ones
(
Shape
)
/
(
tau1
+
1
)
#kernel[0]=1
#Result1[tau1//2:] = fftconvolve(signal1,kernel,mode='full',axes=0)[tau1//2:len(signal1)]
Result1
=
fftconvolve
(
signal1
,
kernel
,
mode
=
'
full
'
,
axes
=
0
)[:
len
(
signal1
)]
fp
=
tau1
lp
=
len
(
signal1
)
if
verbose
:
plt
.
figure
(
1
)
#plt.clf()
#plt.subplot(121)
plt
.
plot
(
signal1
,
'
k-
'
)
plt
.
plot
(
Result1
,
'
r-
'
)
return
Result1
,
fp
,
lp
#%%
def
AntiCausalBox
(
signal1
,
tau1
,
verbose
=
False
):
signal1
=
np
.
squeeze
(
signal1
)
Shape
=
list
(
np
.
shape
(
signal1
))
Shape
[
0
]
=
tau1
+
1
kernel
=
np
.
ones
(
Shape
)
/
(
tau1
+
1
)
#kernel[-1]=1
Result1
=
fftconvolve
(
signal1
,
kernel
,
mode
=
'
full
'
,
axes
=
0
)[
tau1
:]
fp
=
0
lp
=
len
(
signal1
)
-
tau1
if
verbose
:
plt
.
figure
(
1
)
plt
.
clf
()
#plt.subplot(121)
plt
.
plot
(
signal1
,
'
k-
'
)
plt
.
plot
(
Result1
[:
-
tau1
],
'
r-
'
)
return
Result1
,
fp
,
lp
#%%
def
Poor
(
signal1
,
tau1
,
verbose
=
False
):
signal1
=
np
.
squeeze
(
signal1
)
Result1
=
np
.
zeros
(
signal1
.
shape
)
#Result1[tau1:]=signal1[tau1:]-signal1[:-tau1]
Shape
=
list
(
np
.
shape
(
signal1
))
Shape
[
0
]
=
2
*
tau1
+
1
kernel
=
-
np
.
ones
(
Shape
)
/
(
2
*
tau1
)
kernel
[
tau1
]
=
1
Result1
=
fftconvolve
(
signal1
,
kernel
,
mode
=
'
full
'
,
axes
=
0
)[
tau1
:
-
tau1
]
fp
=
tau1
lp
=
len
(
signal1
)
-
tau1
if
verbose
:
plt
.
figure
(
1
)
plt
.
clf
()
#plt.subplot(121)
plt
.
plot
(
signal1
,
'
k.-
'
)
plt
.
plot
(
Result1
,
'
r-
'
)
return
Result1
,
fp
,
lp
#%%
def
CausalPoor
(
signal1
,
tau1
,
verbose
=
False
,
order
=
[]):
#Result1=np.zeros(signal1.shape)
#Result1[tau1:]=signal1[tau1:]-signal1[:-tau1]
Shape
=
list
(
np
.
shape
(
signal1
))
Shape
[
0
]
=
tau1
+
1
kernel
=
-
np
.
ones
(
Shape
)
/
tau1
kernel
[
0
]
=
1
#Result1[tau1//2:] = fftconvolve(signal1,kernel,mode='full',axes=0)[tau1//2:len(signal1)]
Result1
=
fftconvolve
(
signal1
,
kernel
,
mode
=
'
full
'
,
axes
=
0
)[:
len
(
signal1
)]
fp
=
tau1
lp
=
len
(
signal1
)
if
verbose
:
plt
.
figure
(
1
)
#plt.clf()
#plt.subplot(121)
plt
.
plot
(
signal1
,
'
k-
'
)
plt
.
plot
(
Result1
,
'
r-
'
)
return
Result1
,
fp
,
lp
#%%
def
AntiCausalPoor
(
signal1
,
tau1
,
verbose
=
False
):
Shape
=
list
(
np
.
shape
(
signal1
))
Shape
[
0
]
=
tau1
+
1
kernel
=
-
np
.
ones
(
Shape
)
/
tau1
kernel
[
-
1
]
=
1
Result1
=
fftconvolve
(
signal1
,
kernel
,
mode
=
'
full
'
,
axes
=
0
)[
tau1
:]
fp
=
0
lp
=
len
(
signal1
)
-
tau1
if
verbose
:
plt
.
figure
(
1
)
plt
.
clf
()
#plt.subplot(121)
#plt.plot(signal1,'k-')
plt
.
plot
(
Result1
[:
-
tau1
],
'
r-
'
)
return
Result1
,
fp
,
lp
#%%
def
Incr
(
signal1
,
tau1
,
verbose
=
False
):
signal1
=
np
.
squeeze
(
signal1
)
N
=
np
.
shape
(
signal1
)[
0
]
Result1
=
np
.
zeros
(
signal1
.
shape
)
Result1
[
tau1
:]
=
signal1
.
take
(
indices
=
range
(
tau1
,
N
),
axis
=
0
)
-
signal1
.
take
(
indices
=
range
(
0
,
N
-
tau1
),
axis
=
0
)
fp
=
tau1
lp
=
len
(
signal1
)
if
verbose
:
plt
.
figure
(
1
)
plt
.
clf
()
#plt.subplot(121)
plt
.
plot
(
signal1
,
'
k.-
'
)
plt
.
plot
(
Result1
,
'
r-
'
)
return
Result1
,
fp
,
lp
def
AntiIncr
(
signal1
,
tau1
,
verbose
=
False
):
signal1
=
np
.
squeeze
(
signal1
)
N
=
np
.
shape
(
signal1
)[
0
]
Result1
=
np
.
zeros
(
signal1
.
shape
)
Result1
[:
-
tau1
]
=
-
signal1
.
take
(
indices
=
range
(
tau1
,
N
),
axis
=
0
)
+
signal1
.
take
(
indices
=
range
(
0
,
N
-
tau1
),
axis
=
0
)
fp
=
0
lp
=
len
(
signal1
)
-
tau1
if
verbose
:
plt
.
figure
(
1
)
plt
.
clf
()
#plt.subplot(121)
plt
.
plot
(
signal1
,
'
k.-
'
)
plt
.
plot
(
Result1
,
'
r-
'
)
return
Result1
,
fp
,
lp
#%%
def
SymIncr
(
signal1
,
tau1
,
verbose
=
False
):
signal1
=
np
.
squeeze
(
signal1
)
N
=
np
.
shape
(
signal1
)[
0
]
Result1
=
np
.
zeros
(
signal1
.
shape
)
Result1
[
tau1
:
-
tau1
]
=
2
*
signal1
.
take
(
indices
=
range
(
tau1
,
N
-
tau1
),
axis
=
0
)
-
signal1
.
take
(
indices
=
range
(
0
,
N
-
2
*
tau1
),
axis
=
0
)
-
signal1
.
take
(
indices
=
range
(
2
*
tau1
,
N
),
axis
=
0
)
Result1
[
tau1
:
-
tau1
]
=
2
*
signal1
[
tau1
:
-
tau1
]
-
signal1
[:
-
2
*
tau1
]
-
signal1
[
2
*
tau1
:]
fp
=
tau1
lp
=
len
(
signal1
)
-
tau1
if
verbose
:
plt
.
figure
(
1
)
plt
.
clf
()
#plt.subplot(121)
plt
.
plot
(
signal1
,
'
k.-
'
)
plt
.
plot
(
Result1
,
'
r.-
'
)
return
Result1
,
fp
,
lp
def
LPT
(
signal1
,
tau1
,
verbose
=
False
,
order
=
[]):
#Same as Box
#Result1=np.zeros(signal1.shape)
#Result1[tau1:]=signal1[tau1:]-signal1[:-tau1]
signal1
=
np
.
squeeze
(
signal1
)
#Result1[tau1:]=signal1[tau1:]-signal1[:-tau1]
Shape
=
list
(
np
.
shape
(
signal1
))
Shape
[
0
]
=
tau1
+
1
kernel
=
np
.
ones
(
Shape
)
/
(
tau1
+
1
)
#kernel[0]=1
#Result1[tau1//2:] = fftconvolve(signal1,kernel,mode='full',axes=0)[tau1//2:len(signal1)]
Result1
=
fftconvolve
(
signal1
,
kernel
,
mode
=
'
full
'
,
axes
=
0
)[:
len
(
signal1
)]
fp
=
tau1
lp
=
len
(
signal1
)
if
verbose
:
plt
.
figure
(
1
)
#plt.clf()
#plt.subplot(121)
plt
.
plot
(
signal1
,
'
k-
'
)
plt
.
plot
(
Result1
,
'
r-
'
)
return
Result1
,
fp
,
lp
def
HPT
(
signal1
,
tau1
,
verbose
=
False
,
order
=
[]):
fp
=
tau1
lp
=
len
(
signal1
)
Result1
=
signal1
-
LPT
(
signal1
,
tau1
)[
0
]
if
verbose
:
plt
.
figure
(
1
)
#plt.clf()
#plt.subplot(121)
plt
.
plot
(
signal1
,
'
k-
'
)
plt
.
plot
(
Result1
,
'
r-
'
)
return
Result1
,
fp
,
lp
def
BPT
(
signal1
,
tau1
,
tau2
,
verbose
=
False
,
order
=
[]):
fp
=
tau1
lp
=
len
(
signal1
)
Result1
=
LPT
(
signal1
,
tau1
)[
0
]
-
LPT
(
signal1
,
tau2
)[
0
]
if
verbose
:
plt
.
figure
(
1
)
#plt.clf()
#plt.subplot(121)
plt
.
plot
(
signal1
,
'
k-
'
)
plt
.
plot
(
Result1
,
'
r-
'
)
return
Result1
,
fp
,
lp
def
BPFreq
(
signal1
,
tau1
,
tau2
,
verbose
=
False
):
signal1
=
np
.
squeeze
(
signal1
)
Result1
=
np
.
zeros
(
signal1
.
shape
)
fp
=
tau1
lp
=
len
(
signal1
)
-
tau1
minfreq
=
1
/
tau2
maxfreq
=
1
/
tau1
fftsignal
=
np
.
fft
.
fft
(
signal1
,
axis
=
0
)
freq
=
np
.
fft
.
fftfreq
(
np
.
size
(
signal1
,
axis
=
0
))
Mask
=
(
np
.
abs
(
freq
)
>=
minfreq
).
astype
(
int
)
*
(
np
.
abs
(
freq
)
<=
maxfreq
).
astype
(
int
)
fftfiltered
=
(
fftsignal
.
T
*
Mask
).
T
Result1
=
np
.
fft
.
ifft
(
fftfiltered
,
axis
=
0
)
if
verbose
:
plt
.
figure
(
1
)
plt
.
clf
()
#plt.subplot(121)
plt
.
plot
(
signal1
,
'
k.-
'
)
plt
.
plot
(
Result1
,
'
r-
'
)
return
Result1
,
fp
,
lp
def
BPOrder
(
signal1
,
tau1
,
tau2
,
order
=
2
,
verbose
=
False
):
signal1
=
np
.
squeeze
(
signal1
)
Result1
=
np
.
zeros
(
signal1
.
shape
)
fp
=
tau1
lp
=
len
(
signal1
)
-
tau1
minfreq
=
1
/
tau2
maxfreq
=
1
/
tau1
fftsignal
=
np
.
fft
.
fft
(
signal1
,
axis
=
0
)
freq
=
np
.
fft
.
fftfreq
(
np
.
size
(
signal1
,
axis
=
0
))
MaskHP
=
(
1j
*
(
freq
/
minfreq
)
**
order
)
/
(
1
+
1j
*
(
freq
/
minfreq
)
**
order
)
MaskLP
=
1
/
(
1
+
1j
*
(
freq
/
maxfreq
)
**
order
)
Mask
=
MaskLP
*
MaskHP
fftfiltered
=
(
fftsignal
.
T
*
Mask
).
T
Result1
=
np
.
fft
.
ifft
(
fftfiltered
,
axis
=
0
)
if
verbose
:
plt
.
figure
(
1
)
plt
.
clf
()
#plt.subplot(121)
plt
.
plot
(
signal1
,
'
k.-
'
)
plt
.
plot
(
Result1
,
'
r-
'
)
return
Result1
,
fp
,
lp
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment