Baud rate and sampling rate

Go To Last Post
11 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Hello,

 

I have been writing the code for reading ADC value and sending it to laptop, using ATxmega128A1U. 

I use ADC and USART modules and I need a help with not messing up Nyquist sampling theorem.

 

 So, I have been using following code to initialize modules:

 

#define BSel 289
#define BScale -7

 

void init_ADC()
{
    ADCA.CH0.MUXCTRL = ADC_CH_MUXPOS_PIN4_gc ;// vcc
    ADCA_REFCTRL = 0x30; //external reference ArefB
    ADCA_PRESCALER = 0x07;
    ADCA_CTRLA = ADC_ENABLE_bm | ADC_CH0START_bm;   //   0b 0000 0111 common request for ACD 0,1
    ADCA_CTRLB = 0x0C; //  12 bit signed (00011100 for 8 bit signed)
    PORTA_DIRCLR = PIN4_bm; //pin 4 as an input
    ADCA_EVCTRL = 0x01;
    ADCA.CH0.CTRL = ADC_CH_INPUTMODE_SINGLEENDED_gc; //singled ended , zero gain. start conversion.
}

 

void USART_init() {
    USARTD0_CTRLB = 0x18; //receiver /transmitter pins enable
    USARTD0_CTRLC = 0x03; //00 for asynchronous, 
    USARTD0_BAUDCTRLA = (BSel & 0xFF); //setting Baud rate
    USARTD0_BAUDCTRLB = ((BScale << 4) & 0xF0) | ((BSel >> 8) & 0x0F);
}

 

I didn't change any peripheral clock nor MCU internal clock, so they are at their default assignments.   

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Also, I am using Atmel Studio terminal, and baud rate of 38400.

 

My partner wrote the code, and now I need to analyze matlab data, but I don't know if the code is actually right in terms of all rates. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

With baud rate of 38400, your effective byte rate with N81 serial settings is 3840. If you send raw bytes, then you have 2 bytes per sample so your given baud rate COULD support 1920 (or slower) samples per second. If you convert to ASCII hex, then you halve that to 910 samples per second or less. If the sample rate is higher than these numbers, the serial channel will not carry all the bytes and will drop something, sometime.

 

This consideration has nothing to do with Nyquist sampling limits. That "law" essentially says that you must sample at least twice as fast as the highest frequency component in the phenomenon being observed.

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

Last Edited: Wed. Jun 14, 2017 - 05:42 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

As far as I understood:

  • sampling frequency of ADC depends on peripheral clock (which is by default 2MHz). If it is prescaled, as in the example above, it means the sampling frequency is

2MHz/ 512 = 3906Hz.  (I also have several simple for loops delay that actually don't tell anything about the period which are placed before and after "sending" function.)

  • But, all in all, I should decrease ADC sampling rate, or increase baud rate. Sampling rate could be decreased since the frequency from sensor is less than 100 Hz.
  • Baud rate is set in USARTD0_BAUDCTRLA and USARTD0_BAUDCTRLB
  • Baud rate is also set in terminal settings, so I am confused with that, if the baud rate in these registers and this baud rate in terminal are the same.

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

At a sample rate of 3906 per second, your baud rate is slightly shy on speed. So, you would either need to increase the baud rate or decrease the sample rate, one or the other.

 

Since your sensor is rated at 100Hz, the Nyquist Theorem simply says that you need to sample at 200Hz or faster. The 200Hz is the absolute minimum. Generally, I would set the practical minimum sample rate at 4X, or 400Hz. You CAN sample faster, then do a running average, to reduce the noise level in the data. For example, if you sample at 1000Hz and do a 4-bin running average, you get a system bandwidth of (about) 250Hz (downstream from the sensor), the Nyquist rule is satisfied, and you get a lower noise level by a factor of 2 (square root of the average size).

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

Last Edited: Wed. Jun 14, 2017 - 06:14 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I see. 

So if I do 4-bin running average, sampling frequency would decrease by ~4, although I am actually sampling at 1kHz(for example). Because at this point, ADC clock that I have is at its lowest. (I divided peripheral clock with the highest prescaler and the peripheral clock frequency is at it's lowest too, making the lowest combination).

 

But to decrease sampling frequency "manually" I should do running average, and in that way the relation to baud rate should be enough.

 

 

Can you, please, confirm that I understood this correctly?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I think that is correct. Not familiar with XMega details.

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you, I will try to find the help for the actual XMega details with baud rate, but all in all sampling rate needs to be less than baud rate.r

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Correct.

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you, Sir.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ooops, NO! That is not the limit. Here is the compuation:

 

1. With N81 serial data, there are 10 serial bits for every 8 bits of data (start bit and stop bit are the extra ones). So, with this setting the BYTE rate is Baud/10.

 

2. If you send RAW data from an ADC that has more than 8 bits but 16 or fewer bits, then you have 2 data bytes per sample. If those data bytes are ASCII encoded, then there are 4 data bytes per sample. 

 

3. Thus, at a sample rate of S per second, there are 2S (raw data) or 4S (ASCII data) BYTES per second.

 

4. Thus Baud/10 must be higher than 2S (raw data) or 4S (ASCII data). In other words, Baud > 20S (raw) or Baud > 40S (ASCII).

 

The confusion arises because Baud is a BIT rate while sample rate generates 2 or more BYTES per sample.

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net