Add solution for rep-20

This commit is contained in:
Tobias Eidelpes 2021-05-14 20:59:39 +02:00
parent cfaa09bf4b
commit 410d3131b5

61
ex3/rep-20.py Normal file
View File

@ -0,0 +1,61 @@
import pandas as pd
import statistics as stat
import numpy as np
import matplotlib.pyplot as plt
import math as m
dataset = pd.read_csv('csv/team13_protocol.csv').fillna(0)
p6_pkts = dataset['6 · # Packets'].tolist()
#return number of elements in p6_pkts list
n = len(p6_pkts)
#calculate median
median = stat.median(p6_pkts)
#in case of 0 values, replace it with the median
p6_pkts = [ p6_pkts[i] if p6_pkts[i] !=0 else median for i in range(0,n) ]
pkt_fft = np.fft.fft(p6_pkts) #calculates the fft
pkt_amp = np.abs(pkt_fft) #returns absolute values
k = range(0,n-1) #creates an array from 0 to n-1
#set k values in the x-axis and specify the limit
x = k[1:m.floor(n/2)]
#set amp. values in the y-axis and specify the limit
y = pkt_amp[1:m.floor(n/2)]
plt.stem(x, y)
#find max index and value
max_k = np.flip(np.argsort(pkt_amp[1:m.floor(n/2)]))[0]
max_amp = pkt_amp[1:m.floor(n/2)][max_k]
plt.xlim(1, m.floor(n/2))
plt.xlabel('k') #sets x-axis label
#set y-axis label
plt.ylabel('Amplitude [millions of pkts]')
plt.title('Amp. Spectrum for #pkts') #displays title
print('TCP #pkts/hour - FFT max value: ', round(max_amp / 1000000, 2))
print('TCP #pkts/hour - k of FFT max value: ', np.where(pkt_amp == max_amp))
print('TCP #pkts/hour - period of k corresponding to FFT max value: ', n / 5)
p6_uIPs = dataset['6 · # Unique Source IPs'].tolist()
#return number of elements in p6_pkts list
n_2 = len(p6_uIPs)
#calculate median
median_2 = stat.median(p6_uIPs)
#in case of 0 values, replace it with the median
p6_uIPs = [ p6_uIPs[i] if p6_uIPs[i] !=0 else median_2 for i in range(0,n_2) ]
uIPs_fft = np.fft.fft(p6_uIPs) #calculates the fft
uIPs_amp = np.abs(uIPs_fft) #returns absolute values
k = range(0,n_2-1) #creates an array from 0 to n-1
#set k values in the x-axis and specify the limit
x = k[1:m.floor(n_2/2)]
#set amp. values in the y-axis and specify the limit
y = pkt_amp[1:m.floor(n/2)]
plt.stem(x, y)
#find max index and value
max_k = np.flip(np.argsort(uIPs_amp[1:m.floor(n_2/2)]))[0]
max_amp = uIPs_amp[1:m.floor(n_2/2)][max_k]
plt.xlim(1, m.floor(n_2/2))
plt.xlabel('k') #sets x-axis label
#set y-axis label
plt.ylabel('Amplitude [millions of unique source IPs]')
plt.title('Amp. Spectrum for unique source IPs') #displays title
print('TCP #uIPs/hour - FFT max value: ', round(max_amp / 1000000, 2))
print('TCP #uIPs/hour - k of FFT max value: ', np.where(uIPs_amp == max_amp))
print('TCP #uIPs/hour - period of k corresponding to FFT max value: ', n_2 / 31)