-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapodise.py.save
57 lines (45 loc) · 1.82 KB
/
apodise.py.save
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
50
51
52
53
54
55
56
57
# -*- coding: utf-8 -*-
"""
Created on Wed Sep 12 17:34:35 2018
@author: Peter
"""
from scipy import interpolate, fftpack, signal
import numpy as np
# Function to Fourier filter spectra
class apodiser():
def fftspec(self,spectrum):
fft = fftpack.fft(spectrum[:,1])
filtered_fft = fft.copy()
filtered_fft[100:]=0 # Can play around with this value (100) to filter more or less
filtered_spectrum = fftpack.ifft(filtered_fft)
spectrum[:,1] = filtered_spectrum
return spectrum
# Function to fit a spline to a spectrum and return a spectrum normalised
# about zero
def splinespec(self,spectrum):
spline_fit = interpolate.UnivariateSpline(spectrum[:,0],spectrum[:,1])
normalised_spectrum = (spectrum[:,1]/spline_fit(spectrum[:,0]))-1
spectrum[:,1] = normalised_spectrum
return spectrum
def mfilt (self,spectrum):
filter = np.ones(len(spectrum))
length =len(spectrum)
for i in range(0,int(length*0.1)):
filter[i] = i/(length*0.1)
for i in range(int(length*0.90),length):
filter[i] = (float(length-i)/((length-(length*0.90))+1))
return filter
def hanwin(self,spectrum):
endlength = int(0.05*len(spectrum))
cosine = signal.hann(endlength*2)
hann = np.on(len(spectrum))
hann[0:endlength] = cosine[0:endlength]
hann[-endlength:] = cosine[endlength:endlength*2]
hann = spectrum * hann
return hann
def apodise(self, spectrum):
spectrum = self.splinespec(spectrum)
spectrum = self.fftspec(spectrum)
spectrum = self.mfilt(spectrum)
spectrum = self.hanwin(spectrum)
return spectrum