forked from dhoelzer/ShowMeThePackets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fft.py
executable file
·64 lines (56 loc) · 1.75 KB
/
fft.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import numpy as np
from spectrum import *
import matplotlib.pyplot as plt
import sys
import argparse
parser = argparse.ArgumentParser(description="Generate time domain and frequency domain plots of the input data.")
parser.add_argument('-w','--window', default=60.0, type=float, dest="window", help="Define the time window for the time domain histogram and frequency analysis.")
window = parser.parse_args().window
bin=0
startingTime = 0.0
endingTime = 0.0
binLimit=-1.0
samples = 0
timeDomain = []
for line in sys.stdin:
(timestamp, host)=line.split(" ")
ts = float(timestamp)
if(binLimit == -1.0):
startingTime = ts
binLimit = ts + window
samples = 1
timeDomain.append(0)
while(ts > binLimit):
timeDomain.append(0)
samples += 1
bin += 1
binLimit += window
if(ts <= binLimit):
timeDomain[bin] += 1
samples += 1
endingTime = ts
if bin % 2 == 1:
bin+=1
timeDomain.append(0)
(f, plots) = plt.subplots(3)
seconds = [float(n) * window for n in range(0,len(timeDomain))]
plots[0].plot(seconds,timeDomain)
plots[0].set_title("Packets over time")
plots[0].set(xlabel="Seconds")
freqDomain = np.fft.fft(np.array(timeDomain))
samplingFrequency = 1.0/float(window)
n=len(freqDomain)
frequencies = np.fft.fftfreq(n, 1/window)
frequencyIndices = frequencies
fwindow = np.hanning(len(timeDomain))
fd = np.fft.fft(np.array(timeDomain) * fwindow)
plots[1].plot(frequencyIndices,fd)
plots[1].set_title("Frequencies")
plots[1].set(xlabel="Frequency")
p = speriodogram(np.array(timeDomain), len(timeDomain), sampling=window)
plots[2].plot(p)
plots[2].set(xlabel="Power")
#plots[1].plot(frequencyIndices,freqDomain)
#plots[1].set_title("Frequencies")
#plots[1].set(xlabel="Frequency")
plt.show()