Skip to content
Snippets Groups Projects
Commit 86362879 authored by FROGE Ewen's avatar FROGE Ewen
Browse files

Replace Main.py

parent d8f5eb2e
No related branches found
No related tags found
No related merge requests found
......@@ -53,7 +53,7 @@ results = {}
def gaussian_kernel(scale, position):
size=2*scale+1
x = np.linspace(-size // 2, size // 2, size)
x = np.linspace(-(size//2), size // 2, size)
kernel = np.exp(-0.5 * (x / scale) ** 2)
if position == 'causal':
......@@ -68,6 +68,101 @@ def gaussian_kernel(scale, position):
kernel /= kernel.sum()
return kernel
def gaussian_HP_kernel(scale, position):
size=2*scale+1
x = np.linspace(-(size//2), size // 2, size)
kernel = np.exp(-0.5 * (x / scale) ** 2)
if position == 'causal':
kernel[:scale] = 0
elif position == 'anticausal':
kernel[scale + 1:] = 0
elif position == 'symmetric':
pass
kernel /= kernel.sum()
delta=np.zeros(size)
delta[scale]=1
kernel=delta-kernel
return kernel
def box_HP_kernel(scale, position):
size=2*scale+1
kernel = np.ones(size)
if position == 'causal':
kernel[:scale] = 0
elif position == 'anticausal':
kernel[scale + 1:] = 0
elif position == 'symmetric':
pass
kernel /= kernel.sum()
delta=np.zeros(size)
delta[scale]=1
kernel=delta-kernel
return kernel
def gaussian_BP_kernel(scale_high,position,scale_low=None):
if scale_low==None:
scale_low=2*scale_high
size=2*scale_low+1
x = np.linspace(-(size//2), size // 2, size)
kernel_low = np.exp(-0.5 * (x / scale_low) ** 2)
kernel_high = np.exp(-0.5 * (x / scale_high) ** 2)
if position == 'causal':
kernel_low[:size//2] = 0
kernel_high[:size//2] = 0
elif position == 'anticausal':
kernel_low[size//2 + 1:] = 0
kernel_high[size//2 + 1:] = 0
elif position == 'symmetric':
pass
kernel_low /= kernel_low.sum()
kernel_high /= kernel_high.sum()
kernel=kernel_low-kernel_high
return kernel
def box_BP_kernel(scale_high,position,scale_low=None ):
if scale_low==None:
scale_low=2*scale_high
size=2*scale_low+1
x = np.linspace(-(size//2), size // 2, size)
kernel_low = np.zeros(size)
kernel_low[np.where(np.abs(x)<=scale_low)]=1
kernel_high = np.zeros(size)
kernel_high[np.where(np.abs(x)<=scale_low)]=1
if position == 'causal':
kernel_low[:size//2] = 0
kernel_high[:size//2] = 0
elif position == 'anticausal':
kernel_low[size//2 + 1:] = 0
kernel_high[size//2 + 1:] = 0
elif position == 'symmetric':
pass
kernel_low /= kernel_low.sum()
kernel_high /= kernel_high.sum()
kernel=kernel_low-kernel_high
return kernel
def increment_kernel(scale,position):
if position == 'causal':
kernel = np.zeros(2 * scale + 1)
......@@ -92,7 +187,7 @@ def gabor_kernel(scale, position,omega=None):
if omega==None:
omega=1/scale
size=2*scale+1
x = np.linspace(-size // 2, size // 2, size)
x = np.linspace(-(size//2), size // 2, size)
if position == 'causal':
......@@ -128,7 +223,10 @@ kernel_functions = {
'increment': increment_kernel,
'gaussian': gaussian_kernel,
'gabor': gabor_kernel,
'box': box_kernel
'box': box_kernel,
'box_HP': box_HP_kernel,
'gaussian_HP': gaussian_HP_kernel,
}
kernel_types = kernel_functions.keys()
......@@ -207,7 +305,12 @@ for kernel_type in kernel_types:
# %%
# Plotting impulse response for each filter
# Impulse Response Plotting
#kernel_types=['increment','box','gaussian','gabor']
kernel_types=['box_HP','gaussian_HP']
#kernel_types=['box_BP','gaussian_BP']
impulse = np.zeros(100)
impulse[len(impulse) // 2] = 1
......@@ -225,10 +328,11 @@ for i, kernel_type in enumerate(kernel_types):
kernel = kernel_functions[kernel_type](scale, position)
impulse_response = convolve(impulse, kernel, mode='same')
ax.plot(impulse_response, label=f'Scale={scale}')
ax.axhline(0, color='black', linestyle='--') # Add horizontal line at 0
ax.legend(fontsize=24)
plt.tight_layout(rect=[0, 0, 1, 0.95])
plt.savefig('/users2/local/e22froge/codes/TE_Filter/Impulse_responses.pdf', bbox_inches='tight')
plt.savefig('/users2/local/e22froge/codes/TE_Filter/Impulse_responses_HP.pdf', bbox_inches='tight')
plt.show()
#%%
......@@ -245,10 +349,11 @@ for i, kernel_type in enumerate(kernel_types):
for scale in scales:
kernel = kernel_functions[kernel_type](scale, position)
ax.plot(kernel, label=f'Scale={scale}')
ax.axhline(0, color='black', linestyle='--') # Add horizontal line at 0
ax.legend(fontsize=24)
plt.tight_layout(rect=[0, 0, 1, 0.95])
plt.savefig('/users2/local/e22froge/codes/TE_Filter/Kernels.pdf', bbox_inches='tight')
plt.savefig('/users2/local/e22froge/codes/TE_Filter/Kernels_HP.pdf', bbox_inches='tight')
plt.show()
# %%
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment