How to I get this Kalman filter to work for my data?

3 views (last 30 days)
The cj.csv works, but the EvanRocketDrop_final.csv does not... Don't know if this is against any forum rules, but I can give $20 via Paypal to anyone who figures this out. We're kind of running low on time, and it's not a huge priority, but it would be very very nice to have for our project. It's important to know that the .csv files that the Kalmanfilter.m works with are sampled at 1000 Hz while ours is about 160 Hz.
  2 Comments
Geoff Hayes
Geoff Hayes on 31 Jul 2014
Thomas - are you the author of the Kalman filter code (Kalmanfilter.m)? Please elaborate on what you mean how to get this Kalman filter to work for my data. What problems are you experiencing? What is supposed to happen? Are you tracking a target, or multiple targets? Have you verified that the data is valid and that it is the filter code that is the culprit?
Please include the code and csv files that you reference in your question if you wish to get some help for this problem.
Thomas
Thomas on 31 Jul 2014
Here are the files. It gives some error code about array sizes. I looked it over so many times, I know the error is small, but I still can't find it.

Sign in to comment.

Answers (1)

Geoff Hayes
Geoff Hayes on 31 Jul 2014
Thomas - when I ran your code with the EvanRocketDrop1._final.csv file, I observed the following error
Index exceeds matrix dimensions.
Error in kalmanfilter (line 204)
aver(i) = mean(cda(1:1000,i));
We know that there is something wrong with line 204, so we can either put a breakpoint here or, in the Command Window, type
dbstop if error
which will launch the debug,r when an error is encountered, at the line that caused the error. This allows us to trouble shoot the problem by examining the state of the variables at the time of the error.
To clear this debugging condition, again in the Command Window, type
dbclear all
So let's assume that we've run dbstop if error and so we re-run your script. The debugger is launched and pauses at line 204
aver(i) = mean(cda(1:1000,i));
What can we observe from this line? i is 2, and cda is an empty 0x7 matrix. That explains the error - we can't possible extract the first 1000 rows from an empty matrix! So why is it empty? At lines 188-190, we initialize cda as
% This new array eliminates the settling down period in the sensor output.
cda(:,:) = ADC(1001:(samples),:);
cda(:,1) = ADC(1:(samples - 1000),1);
which seems reasonable until we look at ADC which is a matrix of dimension 622x7. The size of this matrix is determined by your input data from the EvanRocketDrop1._final.csv file, data, which is again a matrix of size 622x7. So because 1001>622, then the cda matrix is initialized as an empty matrix and the code fails.
The software works fine with the cj.csv file, because the data matrix (once loaded from the file) is of dimension 8150x7, so cda can easily be initialized.
You have already indicated that the cj.csv file was sampled at 1000 Hz, so the settling down period in the sensor output which corresponds to the first 1000 samples is equivalent to the first second of data from the sensor. For the other file, which is sampled at 160 Hz, you probably just want to replace the 1000 with 160. Something like
% This new array eliminates the settling down period in the sensor output.
sdPer = 167;
cda(:,:) = ADC(sdPer+1:(samples),:);
cda(:,1) = ADC(1:(samples - sdPer),1);
and then replace all (or almost all) other instances of 1000 with sdPer. Then just adjust this variable given the sampling rate of the file. Or even better, if we assume that the first column of data corresponds to timestamps, then
sdPer = floor(size(data,1)/(data(end,1)-data(1,1)));
This works very well for the cj.csv file - the number of rows in data is 8150, and the difference in time from the last to the first element is 8.149 seconds (?).
For the EvanRocketDrop_final.csv data file, the same equation fails as the number of rows is 622 but the difference in time from the last to the first element (of the first column) is 4079! What does this first column represent in this data set?
Moreover, plotting the other columns from this data set shows horizontal lines. In fact, if I run the following commands
minColData = min(data(:,2:7));
maxColData = max(data(:,2:7));
minColData==maxColData
ans =
1 1 1 1 1 1
which shows that each of columns 2 through 7 have the identical elements in each row (each column has a different value, but these values are identical for each row).
---------------------------------------------
The code can easily be adjusted to compensate for the different sized data sets, if we assume that it is the first one second of data that we wish to consider as the settling down period. Just use the
sdPer = floor(size(data,1)/(data(end,1)-data(1,1)));
so long as the assumption holds true that the first column contains timestamp data (in seconds) that have been sampled at a fixed rate.
Once that is done, then you will have to review your EvanRocketDrop_final.csv data file as it seems to have invalid data - the timestamp column does not match the sampling rate of 160 Hz, and the data for any one column (2-7) is identical for that column.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!