Skip to content

nickraymond/Spoon_test_02

Repository files navigation

Spoon_test_02

Matlab code to test linear interpolation using GPS data to correct SYS clock drift

To run the spoon_test_02.m file please download the zip file and open up Matlab 2017a or later and add the folder to the working directory. Then uncomment one of the following sets of lines to run different versions of the NNNN_SYS.csv and NNNN_GPScsv files.

    
  % CASE 1: uncomment lines to test algorythm using inputs WITHOUT SYS drift error
%     GPS_file = readtable("NNNN_GPS_ideal.csv"); 
%     SYS_file = readtable("NNNN_SYS.csv");     
%     disp("*** Running algorythm WITHOUT drift error ***");
 
  % CASE 2: uncomment lines to test algorythm using inputs WITH SYS drift error
%     GPS_file = readtable("NNNN_GPS_error.csv");      
%     SYS_file = readtable("NNNN_SYS.csv");             
%     disp("*** Running algorythm WITH drift error ***");


  % CASE 3: uncomment lines to test algorythm using real buoy data 
%     GPS_file = readtable("0872_GPS.csv");       % run data from Buoy
%     SYS_file = readtable("0872_SYS.csv"); % run data from Buoy
%     disp("*** Running algorythm using real buoy data ***");

Output

A new file will be created when you run the program. The name is NNNN_SYS_new.csv.

This new file will have the ard_millis data from the SYS.csv file, along with the new GPS Epoch time using linear interpolation to compensate for any drift. This file can be used to compare with the known master files.

CASE 1: no drift error

Filename: NNNN_GPS_ideal.csv

This is a test file to represent information provided in one of the NNNN_GPS.cv files. It assumes that the SYS clock has NO drift, and therefore the SYS clock and GPS clock are synchronized. A 1,000 millisecond change in SYS is exactly equal to a 1 second change in the GPS time.This is the first test file used to verify that the backward difference linear interpolation algorythm is working.

ard_millis GPS Epoch Time
6000 1489675375
16000 1489675385
32000 1489675401
42000 1489675411

Filename: MASTER.csv

The file named MASTER.csv is what the output of the algorithm should look like when inputting the file NNNN_GPS_ideal.csv and NNNN_SYS.csv.

We see that the algoryth performed the linear interpolation inbetween the specified ard_millis points 6000, 16000, 32000, 42000 and that an even spacing of two seconds is maintained for all the GPS Epoch time values in the table. This was accomplished even though the interval between the NNNN_GPS_ideal.csv values had a non uniform spacing. This is a quick check to verify that the program is working as expected.

ard_millis GPS Epoch Time
0 1489675369
2000 1489675371
4000 1489675373
6000 1489675375
8000 1489675377
10000 1489675379
12000 1489675381
14000 1489675383
16000 1489675385
18000 1489675387
20000 1489675389
22000 1489675391
24000 1489675393
26000 1489675395
28000 1489675397
30000 1489675399
32000 1489675401
34000 1489675403
36000 1489675405
38000 1489675407
40000 1489675409
42000 1489675411
44000 1489675413
46000 1489675415
48000 1489675417
50000 1489675419
52000 1489675421
54000 1489675423
56000 1489675425
58000 1489675427
60000 1489675429

CASE 2: with drift error

Filename: NNNN_GPS_error.csv

This is the second test file which represents information provided in one of the NNNN_GPS.cv files and assumes that the SYS clock HAS a drift error. Therefore the SYS clock and GPS clock are NOT synchronized. A 1,000 millisecond change in SYS is NOT necessarily equal to a 1 second change in the GPS time.

When the spoon_test_02.m file is run using this file, the output file NNNN_SYS_new.csv will have a uniform 2000 millisecond spacing between the entries the first column, but the entries in the second column will NOT always have a uniform 2 second spacing. In this example the SYS clock runs fast, so the true elapsed GPS Epock time is less than would be expected if we only used the SYS clock to keep time.

ard_millis GPS Epoch Time [error]
6000 1489675375
16000 1489675380
32000 1489675400
42000 1489675406

When running the spoon_test_02.m program, the output file will NOT have uniform spacing between the entries in the second column, but the corresponding GPS Epoch times shown in file NNNN_GPS_error.csv (@ 6000, 16000, 32000, 42000 milliseconds) will match those shown in the output file.

This is because the backward difference linear integration will use the values from the NNNN_GPS_error.csv file to calculate a list of scaling factors that are then applied to the entries in the NNNN_SYS.csv files to compensate for the SYS clock drift.

This is done with the following calculation:

GPS_new[n] = (SYS[n] - SYS[n-1])*scale[j]) + GPS[n-1]

where:
 n is an index that can span 1 to size(SYS.csv)
 j is an index that can span 1 to size(GPS.csv)
 
 and: 
 
 scale[j] = (GPS(j+1) - GPS(j)) / (ard_millis(j+1) - ard_millis(j))
 
 

The value "scale" is equivalent to the slope of the linear equation y(x) = m*x+b. Because we are using the backward difference method, one assumption is that the SYS.csv files will always run for a longer duration than the GPS.csv file. The last scaling factor derived from the GPS.csv file is always applied to the last sets of ard_millis data points.

A better solution could be implimenting the central difference method or the forward difference method. In this way, a scaling factor is calcualted based on GPS data before and after the ard_millis event and then averaged together to interpolate. In this way it is possible to compensate for drift by applying the scaling value to the end of the data set even if the GPS.csv file ran for a longer duration that the SYS.csv file.

CASE 3: buoy data

Run the third case to see the output when using real data buoy. An example of the output has been saved as file 0872_SYS_new.csv in the repo.

About

Matlab code to test linear interpolation using GPS data to correct SYS clock drift

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages