-
Notifications
You must be signed in to change notification settings - Fork 0
/
NaNRemover.m
275 lines (234 loc) · 8.13 KB
/
NaNRemover.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
data = uiimport('-file'); % Need to be imported as vectors, imports and stores as a structure called data
fn = fieldnames(data); %array of the names of each coloumn from excel file
for i=1:numel(fn) %loop that cycles through every coloumn of structure
data.(fn{i})(1) = 0; %set first element to 0
for k=1:length(data.(fn{i})) %loop that cycles through every element of every column
if (isnan(data.(fn{i})(k))) %check to see if array element is NaN
newvalue = data.(fn{i})(k-1); %if it is NaN then set the new value to the previous value
n=1; %counter
while (isnan(newvalue)) %loop to keep decreasing the newvalue until it is NaN
newvalue = data.(fn{i})(k-n);
n = n + 1; %increase counter
end
data.(fn{i})(k) = newvalue;
end
end
end
%GENERAL
Lat = (data.x_Latitude___Degrees____180_0_180_0_50_');
Long = (data.x_Longitude___Degrees____180_0_180_0_50_');
Speed = (data.x_Speed___MPH___0_0_150_0_50_');
Time = (data.x_Interval___ms___0_0_1_');
RPM = (data.x_RPM___RPM___0_12500_50_');
Steering_Angle = (data.x_SteeringAng___Volts____105_0_105_0_50_');
Altitude = (data.x_Altitude___ft___0_0_4000_0_50_');
%POTENTIOMETERS
FL_Damper = (data.x_FrontLeft______0_0_100_0_100_');
FR_Damper = (data.x_FrontRight______0_0_100_0_100_');
RL_Damper = (data.x_RearLeft______0_0_100_0_100_');
RR_Damper = (data.x_RearRight______0_0_100_0_100_');
%BRAKES
R_Brake = (data.x_RearBrake___Volts___0_0_5_0_50_');
F_Brake = (data.x_FrontBrake___Volts___0_0_5_0_50_');
%ROLL
Yaw = (data.x_Yaw___Deg_Sec____120_120_25_');
Pitch = (data.x_Pitch___Deg_Sec____120_120_25_');
Roll = (data.x_Roll___Deg_Sec____120_120_25_');
%ACCELERATION
Accel_X = (data.x_AccelX___G____3_0_3_0_25_');
Accel_Y = (data.x_AccelY___G____3_0_3_0_25_');
Accel_Z = (data.x_AccelZ___G____3_0_3_0_25_');
if (0)
Selected_Lat(1) = 40.84479;
Selected_Lat(2) = 40.84487;
Selected_Long(1) = -96.76793;
Selected_Long(2) = -96.76790;
else
f = figure;
scatter(nonzeros(Lat), nonzeros(Long));%#ok<*NASGU>
[Selected_Lat,Selected_Long]=ginput(2);
close(f);
end
if(Selected_Lat(2)<Selected_Lat(1))
temp = Selected_Lat(2);
Selected_Lat(2) = Selected_Lat(1);
Selected_Lat(1) = temp;
end
if(Selected_Long(2)<Selected_Long(1))
temp = Selected_Long(2);
Selected_Long(2) = Selected_Long(1);
Selected_Long(1) = temp;
end
LapNumber = 1;
counter = 0;
index = 1;
%Sampling Frequency => Default is 1
SamplingFreq = 1;
%Cutoff Points => I.e if you need to get rid of garbage data at
%start or finish
SamplingStart = 80; % Needs to be a multiple of freq. I.e if frequency is 4 Start can be 4, 8, 12, etc.
SamplingFinish = 80;
for i = SamplingStart:(length(Yaw) - SamplingFinish)
if (counter == 0) && ((Lat(i) <= Selected_Lat(2)) && (Lat(i) >= Selected_Lat(1))) && ((Long(i) <= Selected_Long(2)) && (Long(i) >= Selected_Long(1)))
LapIndex(LapNumber,1) =index-1;
index = 1;
counter = 8000/SamplingFreq;
LapNumber = LapNumber + 1;
end
%Here the filtered data is
Lat_filtered(index,LapNumber)= Lat(i);
Long_filtered(index,LapNumber)= Long(i);
Speed_filtered(index,LapNumber)= Speed(i);
Time_filtered(index,LapNumber)= Time(i);
RPM_filtered(index,LapNumber)= RPM(i);
Steering_Angle_filtered(index,LapNumber)= Steering_Angle(i);
Altitude_filtered(index,LapNumber)= Altitude(i);
%POTENTIOMETERS
FL_Damper_filtered(index,LapNumber)= FL_Damper(i);
FR_Damper_filtered(index,LapNumber)= FR_Damper(i);
RL_Damper_filtered(index,LapNumber)= RL_Damper(i);
RR_Damper_filtered(index,LapNumber)= RR_Damper(i);
%BRAKES
R_Brake_filtered(index,LapNumber)= R_Brake(i);
F_Brake_filtered(index,LapNumber)= F_Brake(i);
%ROLL
Yaw_filtered(index,LapNumber)= Yaw(i);
Pitch_filtered(index,LapNumber)= Pitch(i);
Roll_filtered(index,LapNumber)= Roll(i);
%ACCELERATION
Accel_X_filtered(index,LapNumber)= Accel_X(i);
Accel_Y_filtered(index,LapNumber)= Accel_Y(i);
Accel_Z_filtered(index,LapNumber)= Accel_Z(i);
index = index + 1;
i = i + SamplingFreq - 1;
if counter ~= 0 %To make sure counter doesn't go below zero... counter should be a multiple of freq
counter = counter - 1;
end
end
LapIndex(LapNumber,1) =index-1;
LapNumber
%Re-saving into new structure
%GENERAL
data_filtered.Latitude = Lat_filtered;
data_filtered.Longitude = Long_filtered;
data_filtered.Speed = Speed_filtered;
data_filtered.Time = Time_filtered;
data_filtered.RPM = RPM_filtered;
data_filtered.Steering_Angle = Steering_Angle_filtered;
data_filtered.Altitude = Altitude_filtered;
%POTENTIOMETERS
data_filtered.FL_Damper = FL_Damper_filtered;
data_filtered.FR_Damper = FR_Damper_filtered;
data_filtered.RL_Damper = RL_Damper_filtered;
data_filtered.RR_Damper = RR_Damper_filtered;
%BRAKES
data_filtered.R_Brake = R_Brake_filtered;
data_filtered.F_Brake = F_Brake_filtered;
%ROLL
data_filtered.Yaw = Yaw_filtered;
data_filtered.Pitch = Pitch_filtered;
data_filtered.Roll = Roll_filtered;
%ACCELERATION
data_filtered.Accel_X = Accel_X_filtered;
data_filtered.Accel_Y = Accel_Y_filtered;
data_filtered.Accel_Z = Accel_Z_filtered;
%LapIndexes
data_filtered.Lap_Index = LapIndex;
struct2csv(data_filtered, 'log_1_cleaned.csv');
clear
function struct2csv(s,fn)
% STRUCT2CSV(s,fn)
%
% Output a structure to a comma delimited file with column headers
%
% s : any structure composed of one or more matrices and cell arrays
% fn : file name
%
% Given s:
%
% s.Alpha = { 'First', 'Second';
% 'Third', 'Fourth'};
%
% s.Beta = [[ 1, 2;
% 3, 4]];
%
% s.Gamma = { 1, 2;
% 3, 4};
%
% s.Epsln = [ abc;
% def;
% ghi];
%
% STRUCT2CSV(s,'any.csv') will produce a file 'any.csv' containing:
%
% "Alpha", , "Beta", ,"Gamma", , "Epsln",
% "First","Second", 1, 2, 1, 2, "abc",
% "Third","Fourth", 3, 4, 3, 4, "def",
% , , , , , , "ghi",
%
% v.0.9 - Rewrote most of the code, now accommodates a wider variety
% of structure children
%
% Written by James Slegers, james.slegers_at_gmail.com
% Covered by the BSD License
%
FID = fopen(fn,'w');
headers = fieldnames(s);
m = length(headers);
sz = zeros(m,2);
t = length(s);
for rr = 1:t
l = '';
for ii = 1:m
sz(ii,:) = size(s(rr).(headers{ii}));
if ischar(s(rr).(headers{ii}))
sz(ii,2) = 1;
end
l = [l,'"',headers{ii},'",',repmat(',',1,sz(ii,2)-1)];
end
l = [l,'\n'];
fprintf(FID, l);
n = max(sz(:,1));
for ii = 1:n
l = '';
for jj = 1:m
c = s(rr).(headers{jj});
str = '';
if sz(jj,1)<ii
str = repmat(',',1,sz(jj,2));
else
if isnumeric(c)
for kk = 1:sz(jj,2)
str = [str,num2str(c(ii,kk), 8),','];
end
elseif islogical(c)
for kk = 1:sz(jj,2)
str = [str,num2str(double(c(ii,kk)), 8),','];
end
elseif ischar(c)
str = ['"',c(ii,:),'",'];
elseif iscell(c)
if isnumeric(c{1,1})
for kk = 1:sz(jj,2)
str = [str,num2str(c{ii,kk}, 8),','];
end
elseif islogical(c{1,1})
for kk = 1:sz(jj,2)
str = [str,num2str(double(c{ii,kk}), 8),','];
end
elseif ischar(c{1,1})
for kk = 1:sz(jj,2)
str = [str,'"',c{ii,kk},'",'];
end
end
end
end
l = [l,str];
end
l = [l,'\n'];
fprintf(FID,l);
end
fprintf(FID,'\n');
end
fclose(FID);
end