# What should be my sampling frequency?

I have an accelerometer that I am reading the x, y, z values for m/s^2. I've written up my code in Python as follows.

while True:
x, y, z = sensor.accelerometer
with open('data.txt', 'a+') as f:
f.write('{},{},{}\n'.format(x, y, z))
f.flush()
f.close()
time.sleep(0.1)


The time.sleep(0.1) says to sleep for 1/10 of a second, so theoretically, in 1 second, I should have 10 samples. Thus, I would assume my sampling rate is 10 samples per second. However, what I have noticed is that I do not get 10 samples per second; there is overhead with the file writing (in actuality, I am doing more stuff than just reading and writing the accelerometer data). In fact, in one second, I get a distribution over the number of samples per second.

Now that I have recorded the data, I need to analyze these signals, but with a lot of the signal processing examples, I see that the sampling frequency is a required parameter.

My question is, what should I set as my sampling frequency?

• Should I put in 10? If so, empirically, I am not consistently (if at all) getting 10 samples per second.
• If I do not use 10, I have a distribution over the number of samples per second, should I just use the mean? If I use the mean number of samples per second as the sampling frequency, do I need to remove samples within a second interval?
• What do I do about a second of interval with less than the mean number of samples?

Any help into these questions is appreciated.

• Usually the sampling rate is set in hardware , which in turn, can be software-configurable. – Ben Jun 29 at 15:42

Much depends on what you want to deduce from your data.

In general, if your sample rate is not uniform, your measurements should be accurately time stamped.

The nearer to an average of 1/10 a second your sample intervals are the better.

A typical hueristic used in Engineering is the 1/10 rule, so if your samples are within 1/100 of a second of 1/10 second sampling, you probably can just assume they are uniformly sampled, but again, it really depends on what your measurement's purpose is.

Again if you have timestamps, there are methods to interpolate your measurements to uniform time increments. There is the timeseries class in Matlab which has a method for this purpose, which is an example of how to treat nonuniform samples.

If you don't have the ability to accurately time stamp data, you could use the average of sample intervals. Like all practical solutions to problems you need to use your own judgement to determine if what you do is adequate.

We really can't make that judgement for you.

• I just want to estimate the position from the acceleration. All data have a timestamp, which is simply milliseconds past epoch. You have a link for the Matlab class? – Jane Wayne Jun 29 at 21:41
• – Stanley Pawlukiewicz Jun 29 at 21:44
• @JaneWayne the method ( or function on a timeseries object) is called resample() – Stanley Pawlukiewicz Jun 29 at 21:59