forked from tyagi-g/EMI-Sense_USRP-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Processing_FFT.m
84 lines (68 loc) · 2.42 KB
/
Processing_FFT.m
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
% Import CSV in to matlab for FFT computation
function Processing_FFT_Ver1_0
%GLOBALS%%%%%%%%%%%%%%%%%%%%%
%Name of folder where trace CSVs are stored
f_number = '.\';
%Location of Book.csv
bookpath = '.\Book1.csv';
%Index of last trace in Book.csv
index1 = 2
buffer(:,:) = zeros(50000,100); %Array to temporarily store fft values
sum_buffer = zeros(50000,1); %Array to average fft values
j = 0; %counter
display(bookpath);
%Read trace numbers from Book.csv
fid = fopen(bookpath,'r');
C = textscan(fid, repmat('%s',1,10), 'delimiter',',', 'CollectOutput',true);
C = C{1};
fclose(fid);
for t_index = 2:index1
%counter variable for loop
j=0;
%Set trace number of current trace to be processed
t_number = (C{t_index,1});
%Path to trace CSVs
path = strcat(f_number,t_number,'.csv');
display(path);
display('Loading csv');
%Read data from trace CSV
Data = csvread(path);
display(length(Data));
% Sampling Frequency
Fs= 10000000;
% Load full datagram from CSV payload to matrix for FFT computation
for index =(1:100)
% Load the input fed datagram to compute FFT
x = Data(1+j:99999+j);
% N is FFT Vector Size
N = length(x);
%FFT calculation starts here
display('FFT Start');
xdft = fft(x); %generate fft
xdft = xdft(1:(N+1)/2); %take positive values
psdx = (1/(Fs*N)).*abs(xdft).^2;
psdx(2:end-1) = 2*psdx(2:end-1);
display('FFT Done');
display(index);
% Freq. range of observation as shown on X-axis
freq = 0:Fs/length(x):Fs/2;
%take log of values and store in buffer array
buffer(:,index) = (10*log10(psdx));
%add fft of one frame to sum array
sum_buffer = sum_buffer + buffer(:,index);
%initiliaze temp variables for next loop iteration
j=j+99999;
N = 0; xdft = 0; psdx = 0;
end
display('FFT Averaging over 100 frames');
%avaeraging fft over 100 frames
sum_buffer = sum_buffer./100;
%Convert dB to dBm
sum_buffer = sum_buffer + 30;
%File name of FFT averaged data CSV
output_path = strcat(f_number,t_number,'_FFT_Averaged.csv');
display(output_path);
%Write FFT data to CSV
csvwrite(output_path,[freq',sum_buffer]);
display('Data Dumped');
end