Abhisek92

Iterative Spectra Filtering

Oct 15th, 2020 (edited)
474
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.74 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': 18})
  9. plt.ion()
  10.  
  11. # Inputs
  12. k_size = 3
  13. max_iter = 20
  14.  
  15. df = pd.read_csv('Sample_Spectra.csv', header=0)
  16. s1 = df[["Wavelength", "S1"]].to_numpy()
  17. s2 = df[["Wavelength", "S2"]].to_numpy()
  18. s3 = df[["Wavelength", "S3"]].to_numpy()
  19. x1 = s1[:, 0]
  20. x2 = s2[:, 0]
  21. x3 = s3[:, 0]
  22. y1 = s1[:, 1]
  23. y2 = s2[:, 1]
  24. y3 = s3[:, 1]
  25.  
  26. y1_flt = y1.copy()
  27. x1_flt = x1.copy()
  28.  
  29. i = 0
  30. sd = np.inf
  31.  
  32. iters = list()
  33. sdevs = list()
  34. bdevs = list()
  35.  
  36. while i < max_iter:
  37.     yy1 = np.convolve(y1_flt, np.ones((k_size,))/k_size, mode='valid')
  38.     xx1 = np.convolve(x1_flt, np.ones((k_size,))/k_size, mode='valid')
  39.  
  40.     f1 = interp1d(x=xx1, y=yy1, kind='linear', fill_value="extrapolate", assume_sorted=False)
  41.     y1_ = f1(x1)
  42.     bdevs.append(np.std(y1_))
  43.     delta_y = np.abs(y1 - y1_)
  44.    
  45.     # mask=(delta_y>=(0.15 * y1_))
  46.     # y1_flt[mask] = 0
  47.    
  48.     mask= (delta_y < np.abs(0.15 * y1_))
  49.     y1_[mask] = 0
  50.     y1_flt += y1_
  51.     y1_flt[np.logical_not(mask)] *= 0.5
  52.     sd = np.std(y1_flt)
  53.     i += 1
  54.  
  55. c_mask = y1==y1_flt
  56. yc = y1[c_mask]
  57. xc = x1[c_mask]
  58.  
  59. fig1 = plt.figure(figsize=(10, 10))
  60. ax1 = fig1.add_subplot(111)
  61. l1 = ax1.scatter(x1, y1, c='red', alpha=0.4, label='$\mathrm{Removed~points~from~Original~Spectra}$')
  62. l2 = ax1.scatter(x1, y1_flt, c='blue', label='$\mathrm{Additional~Points~in~Filtered~Spectra}$')
  63. l3 = ax1.scatter(xc, yc, c='green', label='$\mathrm{Overlapped~Points}$')
  64. ax1.set_xlabel("$\mathrm{Wavelength}~(\mu)$")
  65. ax1.set_ylabel("$\mathrm{Reflectance}$")
  66. ax1.legend(loc=8)
  67.  
  68. input("Press Enter to continue...")
  69.  
Add Comment
Please, Sign In to add comment