-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsmooth_ndh.py
executable file
·49 lines (41 loc) · 1.92 KB
/
smooth_ndh.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from scipy import signal
import numpy as np
def smooth_ndh(input_array,smooth_window,window_type='Boxcar'):
"""
% (C) Nick Holschuh - Amherst College - 2022 (Nick.Holschuh@gmail.com)
% Convolution based smoothing function -- both 1d and 2d
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The inputs are as follows:
%
% input_array -- The dataset to be smoothed
% smooth_window -- an integer value that defines the smoothing window size
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The outputs are as follows:
%
% new_data -- the smoothed data
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
"""
if isinstance(smooth_window,int) == 1:
if len(input_array.shape) == 2:
smoothing_kernel = np.ones((smooth_window, smooth_window))
elif len(input_array.shape) == 1:
smoothing_kernel = np.ones((smooth_window))
smoothing_kernel = smoothing_kernel/np.prod(smoothing_kernel.shape)
else:
smoothing_kernel = smooth_window
if window_type == 'Gaussian':
if len(input_array.shape) == 2:
x = np.linspace(-4,4,smooth_window)
y = np.linspace(-4,4,smooth_window)
xmesh, ymesh = np.meshgrid(x,y);
smoothing_kernel = np.exp(-(np.power(xmesh, 2.) / (2 * np.power(1, 2.)) + np.power(ymesh, 2.) / (2 * np.power(1, 2.))))
elif len(input_array.shape) == 1:
x = np.linspace(-4,4,smooth_window)
smoothing_kernel = np.exp(-np.power(x, 2.) / (2 * np.power(1, 2.)))
input_array[input_array == np.NaN] = 0
first_out = signal.fftconvolve(input_array, smoothing_kernel, mode = 'same')
scalar = signal.fftconvolve(np.ones(input_array.shape), smoothing_kernel, mode = 'same')
out_data = first_out / scalar;
return out_data