#### Important changes to forums and questions

All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.

##### 6 years, 12 months ago. This question has been closed. Reason: PROBLEM SOLVED

## How to Measure a waveform with exactly 32 samples per period?

Hi

I have an ADC set up and is measuring an input voltage waveform and displaying to an LCD. The waveform I am measuring has a period which varies regularly. Even when the period varies I want to get 32 samples for each period of the waveform. Is there any way of setting the ADC to gather 32 samples per period? Do I need to use a Ticker?

I think I need to calculate the period of the waveform each cycle and then divide this period by 32 and use this value to trigger a Ticker? Does anybody have any example code that does this as I'm confused on how to implement it. Any help would be greatly appreciated.

Thanks

## 1 Answer

##### 6 years, 12 months ago.

There is no automatic way to do it. You can calculate the period and then use a ticker, or just oversample alot and interpolate afterwards, but that takes alot of processing power.

If you want it that way you will indeed need to make a function first which gets the period, and then sets the ticker accordingly. But what kind of waveform are you measuring, why does itneed to be 32 samples exactly, and which kind of range of periods do you have?

I am measuring the voltage output of an alternator which varies from around 45Hz to 65Hz therefore the period will vary from around 0.022 to 0.015. I want to measure each period exactly 32 times no matter whether the period changes or not, then I will input each value into an array of size 128 which will give me the data for exactly four wave forms ie 32*4=128 . then I will use this data to calculate the RMS and various other parameters of the input voltage, would you have any example code of how to practically achieve this

posted by 30 Oct 2013Never done something like that, so no example code.

First of all, your demand is impossible. Simple as that, you cannot sample 32 times per period no matter what, since you don't know the period of the waveform in advance. Causality says that is now allowed in this universe ;).

So you will need to have an error margin. What I think might be a nice way to do it, but required a somewhat sinusoidial waveform, is by just starting with a period for 55Hz (luckily your range is fairly small). Wait until you are at your halfway point, rising edge, then start sampling. You take 32 samples, then sample 33 should be your halfway point again. If it is less than that it sampled too fast, if it is more than that, you sampled too slow.

Use a simple PI(D) loop to adjust your sampling period, and after a few periods it should start to walk fairly in sync. You can later on improve it by also using the falling edge, max/min location, etc.

posted by 30 Oct 2013The demand is not impossible simple as that as I am not looking to see into the future and the design you have stated would be highly inaccurate as the period will vary from 55Hz. I will be using the present value of period which is being calculated constantly using a rising edge interrupt and a timer. I then use this data to control the sampling rate for example if the period is 0.02 I divide this by 32 and this is the value in seconds in which I need to call the sampling function. I was looking a method of calling the sampling function at the calculated time?

posted by 02 Nov 2013You want to have 32-samples per period exactly while you only know the period of the previous cycle. That is the definition of having to look in the future.

The design I stated is effectively how a sampling PLL works. It starts at 55Hz, then brings itself to exactly the period you need.

Anyway if you do it like that you need Ticker function.

posted by 02 Nov 2013