Advertisement
Abhisek92

Spectra Filtering

Oct 9th, 2020
1,304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.50 KB | None | 0 0
  1. import numpy as np
  2. import pandas as pd
  3. from matplotlib import pyplot as plt
  4. from scipy.interpolate import interp1d
  5.  
  6. from matplotlib import rc
  7. rc('text', usetex=True, aa=True)
  8. plt.rcParams.update({'font.size': 20})
  9. k_size = 3
  10.  
  11. df = pd.read_csv('Sample_Spectra.csv', header=0)
  12. s1 = df[["Wavelength", "S1"]].to_numpy()
  13. s2 = df[["Wavelength", "S2"]].to_numpy()
  14. s3 = df[["Wavelength", "S3"]].to_numpy()
  15. x1 = s1[:, 0]
  16. x2 = s2[:, 0]
  17. x3 = s3[:, 0]
  18. y1 = s1[:, 1]
  19. y2 = s2[:, 1]
  20. y3 = s3[:, 1]
  21.  
  22. y1_flt = y1.copy()
  23. x1_flt = x1.copy()
  24. for i in range(10):
  25.     yy1 = np.convolve(y1_flt, np.ones((k_size,))/k_size, mode='valid')
  26.     xx1 = np.convolve(x1_flt, np.ones((k_size,))/k_size, mode='valid')
  27.     # xx1 = x1[:yy1.size]
  28.  
  29.     # yy2 = np.convolve(y2, np.ones((k_size,))/k_size, mode='valid')
  30.     # xx2 = np.convolve(x2, np.ones((k_size,))/k_size, mode='valid')
  31.     # xx2 = x2[:yy2.size]
  32.  
  33.     # yy3 = np.convolve(y3, np.ones((k_size,))/k_size, mode='valid')
  34.     # xx3 = np.convolve(x3, np.ones((k_size,))/k_size, mode='valid')
  35.     # xx3 = x3[:yy3.size]
  36.  
  37.     f1 = interp1d(x=xx1, y=yy1, kind='linear', fill_value="extrapolate", assume_sorted=False)
  38.     y1_ = f1(x1)
  39.     delta_y = y1 - y1_
  40.    
  41.     mask=(delta_y>=(0.15 * y1_))
  42.     y1_flt[mask] = 0
  43.     y1_[~mask] = 0
  44.     y1_flt += y1_
  45.  
  46. fig1 = plt.figure(figsize=(16, 9))
  47. ax1 = fig1.add_subplot(111)
  48. l1 = ax1.plot(x1, y1, c='skyblue', label='$\mathrm{Original~Spectra}$')
  49. l2 = ax1.plot(x1, y1_flt, c='orange', alpha=0.8, label='$\mathrm{Filtered~Spectra}$')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement