pyosv.freq.filters

  1import numpy as np
  2
  3def gaussian_filter(fft : np.ndarray, mx : int =0, my : int = 0, sx : int = 1, sy : int = 1, invert : bool = False) -> np.ndarray:
  4    '''
  5        Apply a 2D gaussian filter to the input 2D spectrum
  6        
  7        Prameters:
  8        ---------
  9            - fft : np.ndarray 
 10                2D spectrum to be filtered
 11            - mx : int 
 12                mean of gaussian x function (default : 0)
 13            - my : int
 14                mean of gaussian y function (default : 0)
 15            - sx : int
 16                standard deviation of gaussian x function (default : 1)
 17            = sy : int
 18                standard deviation of gaussian y function (default : 1)
 19            - invert : bool
 20                invert the distribution (default : False)
 21        
 22        Returns:
 23        --------
 24            - filt : np.ndarray
 25                filtered spectrum
 26        
 27        Usage:
 28        ------
 29        ```python
 30        import numpy as np
 31
 32        fft = np.array(
 33            [
 34            [0,   0, 1,   0,0],
 35            [0, 0.5, 1, 0.5,0],
 36            [1,   1, 1,   1,1],
 37            [0, 0.5, 1, 0.5,0],
 38            [0,   0, 1,   0,0],
 39            ]
 40
 41        )
 42
 43        filt = gaussian_filter(fft, mx=0, my=0, sx=1, sy=1, invert=False)
 44        ```
 45
 46        Output:
 47        -------
 48        ```
 49        array([  
 50            [0.        , 0.        , 0.09653235, 0.        , 0.        ],  
 51            [0.        , 0.061975  , 0.14045374, 0.061975  , 0.        ],  
 52            [0.09653235, 0.14045374, 0.15915494, 0.14045374, 0.09653235],  
 53            [0.        , 0.061975  , 0.14045374, 0.061975  , 0.        ],  
 54            [0.        , 0.        , 0.09653235, 0.        , 0.        ]])  
 55        ```
 56    '''
 57
 58    if len(fft.shape) < 2:
 59        raise Exception("Error: the shape of fft must be greater than 2")
 60    
 61    x = np.linspace(-1, 1, fft.shape[0])
 62    y = np.linspace(-1, 1, fft.shape[1])
 63    
 64    x, y = np.meshgrid(x,y)
 65
 66    fxy = 1. / (2. * np.pi * sx * sy) * np.exp(-((x - mx)**2. / (2. * sx**2.) + (y - my)**2. / (2. * sy**2.)))
 67
 68    if invert: fxy = 1-fxy
 69    
 70    filt = fft*fxy
 71    
 72    return filt
 73
 74
 75def lhp_filter(fft, radius=0.5, invert=False):
 76    '''
 77        Apply a low pass or high pass filter to the input 2D spectrum
 78        
 79        Parameters:
 80        -----------
 81            - fft : np.ndarray
 82                2D spectrum to be filtered
 83            - radius : int
 84                size of the filter, ammited range [0-1], will mask to zero fft values lower than radius (default : 0.5)
 85            - invert: bool
 86                invert the distribution (default : False)
 87
 88        Returns:
 89        --------
 90            - fft : np.ndarray
 91                filtered spectrum
 92
 93        
 94        Usage:
 95        ------
 96        ```python
 97        import numpy as np
 98
 99        fft = np.array(
100            [
101            [0,   0, 1,   0,0],
102            [0, 0.5, 1, 0.5,0],
103            [1,   1, 1,   1,1],
104            [0, 0.5, 1, 0.5,0],
105            [0,   0, 1,   0,0],
106            ]
107
108        )
109
110        filt = lhp_filter(fft, radius=0.8, invert=False)
111        ```
112
113        Output:
114        -------
115        ```
116        array([  
117            [0., 0., 1., 0., 0.],  
118            [0., 0., 0., 0., 0.],  
119            [1., 0., 0., 0., 1.],  
120            [0., 0., 0., 0., 0.],  
121            [0., 0., 1., 0., 0.]])  
122        ```
123    '''
124    
125    if len(fft.shape) < 2:
126        raise Exception("Error: the shape of fft must be greater than 2")
127    
128    x = np.linspace(-1, 1, fft.shape[0])
129    y = np.linspace(-1, 1, fft.shape[1])
130    x, y = np.meshgrid(x, y)
131
132    fxy = np.sqrt(x**2 + y**2)
133
134    if invert: fxy = 1-fxy
135
136    fft[fxy<radius] = 0
137    
138    return fft
def gaussian_filter( fft: numpy.ndarray, mx: int = 0, my: int = 0, sx: int = 1, sy: int = 1, invert: bool = False) -> numpy.ndarray:
 4def gaussian_filter(fft : np.ndarray, mx : int =0, my : int = 0, sx : int = 1, sy : int = 1, invert : bool = False) -> np.ndarray:
 5    '''
 6        Apply a 2D gaussian filter to the input 2D spectrum
 7        
 8        Prameters:
 9        ---------
10            - fft : np.ndarray 
11                2D spectrum to be filtered
12            - mx : int 
13                mean of gaussian x function (default : 0)
14            - my : int
15                mean of gaussian y function (default : 0)
16            - sx : int
17                standard deviation of gaussian x function (default : 1)
18            = sy : int
19                standard deviation of gaussian y function (default : 1)
20            - invert : bool
21                invert the distribution (default : False)
22        
23        Returns:
24        --------
25            - filt : np.ndarray
26                filtered spectrum
27        
28        Usage:
29        ------
30        ```python
31        import numpy as np
32
33        fft = np.array(
34            [
35            [0,   0, 1,   0,0],
36            [0, 0.5, 1, 0.5,0],
37            [1,   1, 1,   1,1],
38            [0, 0.5, 1, 0.5,0],
39            [0,   0, 1,   0,0],
40            ]
41
42        )
43
44        filt = gaussian_filter(fft, mx=0, my=0, sx=1, sy=1, invert=False)
45        ```
46
47        Output:
48        -------
49        ```
50        array([  
51            [0.        , 0.        , 0.09653235, 0.        , 0.        ],  
52            [0.        , 0.061975  , 0.14045374, 0.061975  , 0.        ],  
53            [0.09653235, 0.14045374, 0.15915494, 0.14045374, 0.09653235],  
54            [0.        , 0.061975  , 0.14045374, 0.061975  , 0.        ],  
55            [0.        , 0.        , 0.09653235, 0.        , 0.        ]])  
56        ```
57    '''
58
59    if len(fft.shape) < 2:
60        raise Exception("Error: the shape of fft must be greater than 2")
61    
62    x = np.linspace(-1, 1, fft.shape[0])
63    y = np.linspace(-1, 1, fft.shape[1])
64    
65    x, y = np.meshgrid(x,y)
66
67    fxy = 1. / (2. * np.pi * sx * sy) * np.exp(-((x - mx)**2. / (2. * sx**2.) + (y - my)**2. / (2. * sy**2.)))
68
69    if invert: fxy = 1-fxy
70    
71    filt = fft*fxy
72    
73    return filt

Apply a 2D gaussian filter to the input 2D spectrum

Prameters:

- fft : np.ndarray 
    2D spectrum to be filtered
- mx : int 
    mean of gaussian x function (default : 0)
- my : int
    mean of gaussian y function (default : 0)
- sx : int
    standard deviation of gaussian x function (default : 1)
= sy : int
    standard deviation of gaussian y function (default : 1)
- invert : bool
    invert the distribution (default : False)

Returns:

- filt : np.ndarray
    filtered spectrum

Usage:

import numpy as np

fft = np.array(
    [
    [0,   0, 1,   0,0],
    [0, 0.5, 1, 0.5,0],
    [1,   1, 1,   1,1],
    [0, 0.5, 1, 0.5,0],
    [0,   0, 1,   0,0],
    ]

)

filt = gaussian_filter(fft, mx=0, my=0, sx=1, sy=1, invert=False)

Output:

array([  
    [0.        , 0.        , 0.09653235, 0.        , 0.        ],  
    [0.        , 0.061975  , 0.14045374, 0.061975  , 0.        ],  
    [0.09653235, 0.14045374, 0.15915494, 0.14045374, 0.09653235],  
    [0.        , 0.061975  , 0.14045374, 0.061975  , 0.        ],  
    [0.        , 0.        , 0.09653235, 0.        , 0.        ]])  
def lhp_filter(fft, radius=0.5, invert=False):
 76def lhp_filter(fft, radius=0.5, invert=False):
 77    '''
 78        Apply a low pass or high pass filter to the input 2D spectrum
 79        
 80        Parameters:
 81        -----------
 82            - fft : np.ndarray
 83                2D spectrum to be filtered
 84            - radius : int
 85                size of the filter, ammited range [0-1], will mask to zero fft values lower than radius (default : 0.5)
 86            - invert: bool
 87                invert the distribution (default : False)
 88
 89        Returns:
 90        --------
 91            - fft : np.ndarray
 92                filtered spectrum
 93
 94        
 95        Usage:
 96        ------
 97        ```python
 98        import numpy as np
 99
100        fft = np.array(
101            [
102            [0,   0, 1,   0,0],
103            [0, 0.5, 1, 0.5,0],
104            [1,   1, 1,   1,1],
105            [0, 0.5, 1, 0.5,0],
106            [0,   0, 1,   0,0],
107            ]
108
109        )
110
111        filt = lhp_filter(fft, radius=0.8, invert=False)
112        ```
113
114        Output:
115        -------
116        ```
117        array([  
118            [0., 0., 1., 0., 0.],  
119            [0., 0., 0., 0., 0.],  
120            [1., 0., 0., 0., 1.],  
121            [0., 0., 0., 0., 0.],  
122            [0., 0., 1., 0., 0.]])  
123        ```
124    '''
125    
126    if len(fft.shape) < 2:
127        raise Exception("Error: the shape of fft must be greater than 2")
128    
129    x = np.linspace(-1, 1, fft.shape[0])
130    y = np.linspace(-1, 1, fft.shape[1])
131    x, y = np.meshgrid(x, y)
132
133    fxy = np.sqrt(x**2 + y**2)
134
135    if invert: fxy = 1-fxy
136
137    fft[fxy<radius] = 0
138    
139    return fft

Apply a low pass or high pass filter to the input 2D spectrum

Parameters:

- fft : np.ndarray
    2D spectrum to be filtered
- radius : int
    size of the filter, ammited range [0-1], will mask to zero fft values lower than radius (default : 0.5)
- invert: bool
    invert the distribution (default : False)

Returns:

- fft : np.ndarray
    filtered spectrum

Usage:

import numpy as np

fft = np.array(
    [
    [0,   0, 1,   0,0],
    [0, 0.5, 1, 0.5,0],
    [1,   1, 1,   1,1],
    [0, 0.5, 1, 0.5,0],
    [0,   0, 1,   0,0],
    ]

)

filt = lhp_filter(fft, radius=0.8, invert=False)

Output:

array([  
    [0., 0., 1., 0., 0.],  
    [0., 0., 0., 0., 0.],  
    [1., 0., 0., 0., 1.],  
    [0., 0., 0., 0., 0.],  
    [0., 0., 1., 0., 0.]])