Advertisement
Danila_lipatov

autocorrelation_spectrum_python

Dec 15th, 2023 (edited)
732
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.73 KB | None | 0 0
  1. """
  2. If it will be helpful, please like this post
  3. """
  4.  
  5. import pandas as pd
  6. import matplotlib.pyplot as plt
  7. import numpy as np
  8. import scipy.stats
  9. from statsmodels.graphics.tsaplots import plot_acf
  10. #https://scicoding.com/4-ways-of-calculating-autocorrelation-in-python/
  11.  
  12. if __name__ == '__main__':
  13.     df = pd.read_csv('CompanyABCProfit.csv').set_index('Year')
  14.     # fig = plt.figure(figsize=(10, 10))
  15.     # plt.xlabel('Year')
  16.     # plt.ylabel('Profit')
  17.     # df.plot()
  18.     # plt.close()
  19.     values_df = []
  20.     for value in df["Profit(Rs '000)"]:
  21.         values_df.append(value)
  22.     corr_func = (np.correlate(values_df - df.mean()[0], values_df - df.mean()[0], 'full')) / df.var()[0] / len(values_df)
  23.     # fig = plt.figure(figsize=(10, 10))
  24.     corr_function = pd.DataFrame(corr_func).reset_index()
  25.     corr_function['index'] = corr_function['index'] - len(df)
  26.     corr_function = corr_function.set_index('index')
  27.     corr_function.plot()
  28.     # plt.show()
  29.     # plt.close()
  30.     Fs = 1024
  31.     sampling_rate = 30 #signal freq
  32.     Number_of_repeat = 10
  33.     N = int(Number_of_repeat * Fs / sampling_rate) #number of samples
  34.     t_step = 1 / Fs
  35.     t = np.linspace(0, (N - 1) * t_step, N) #time interval
  36.     freq = Fs / N #freq step
  37.     f = np.linspace(0, (N - 1) * freq, N) # freq interval
  38.  
  39.     data = 2 * np.sin(2 * np.pi * sampling_rate * t) + 4 * np.sin(2 * np.pi * 3 * sampling_rate * t)
  40.     fureir = scipy.fft.fft(data)
  41.     fureier_abs = abs(fureir / N)
  42.     data_spec = f[0 : int(N / 2 + 1)]
  43.     furier_abs_plot = 2 * fureier_abs[0 : int(N / 2 + 1)]
  44.     fureier_abs[0] /= 2
  45.     fig, [ax1, ax2] = plt.subplots(nrows=2, ncols=1)
  46.     ax1.plot(t, data)
  47.     ax2.plot(data_spec, furier_abs_plot)
  48.     plt.show()
  49.     stop = 'here'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement