import numpy as np
from scipy import signal

#%% 
#Enter the file path to the data you want to analyse. File 0 is the noise floor in both cases.
path = '' 

file_name = 'SNS_Data'
path_file = path + file_name
data_points = 300000
Navg = 100
time = 1 #second

#Enter file you would like the data to save to
save = ''
save_file = path + save

#comment out the code you would not like to produce the data for.

#%% Code for varying the White noise

variable_name = 'White noise (mVrms)'
textfiles_total = 27 
WN_varied_parameter = np.array([0, 7, 20, 40, 50, 60, 70, 80, 90, 100, 110, 135, 165, 195, 220, 250, 280, 350, 450, 600, 800, 1000, 1500, 2000, 2500, 3000, 3500])[0:textfiles_total-1] #mV
data_type = 'WN'

#%% Code for varying the polarisation angle

variable_name = 'Polarisation Angle (degrees)'
textfiles_total = 73
PA_varied_parameter = np.array([0, -20, -18, -16, -14, -12, -10, -8, -6, -4, -2, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100, 102, 104, 106, 108, 110, 112, 114, 116, 118, 120])[0:textfiles_total-1] #degrees
data_type = 'PA'

#%% Code that converts the raw data to the PSD.

ft_y_array_all = np.zeros((textfiles_total, 150001))
ft_x_array_all = np.zeros((textfiles_total, 150001))

for i in range(textfiles_total):

    y = np.loadtxt('%s/SNS_%s_%s.txt' % (path_file, data_type, i)) 

    y0 = y[0,:]
    freq, Pxx0 = signal.periodogram(y0, data_points)
    Pxx_avg = np.zeros(len(Pxx0))

    for m in np.arange(0,Navg):
        hy = y[m,:]
        freq, hPxx = signal.periodogram(hy, data_points)
        Pxx_avg = Pxx_avg + hPxx
            
    Pxx_avg = Pxx_avg/Navg

    np.savetxt('%s/SNS_%s_%s_x.txt' %(save_file, data_type, i), freq)
    np.savetxt('%s/SNS_%s_%s_y.txt' %(save_file, data_type, i), Pxx_avg)