user1711: To make an oscilloscope in matlab environment, for data acquisition on arduino side, when inbuilt analogRead() is used, it's working fine, but with very low sampling rate, that aliasing is clearly observed. That too, non-uniform sampling. Thus listing out the drawbacks: 

Very slow data rate, even if, sent with 115200 baudrate,
Aliasing
Non-uniform sampling


Thus, to overcome above, ADC in free running mode was used, with 16 as prescaling factor, thus 76.9KHz sampling rate (counting each ADC as 13 clock cycles). Thus, data is uniformly sampled now. Also it is quite fast, because of sampling, and is observed. The only problem now exists is, output has a specific noise. The data being sampled is 50Hz sinusoidal current waveform by current sensor ACS712. It overlays instantaneous current waveform over a bias of 2.5V. Thus, when signal is not present, a constant voltage of 2.5V is observed, thus, 512, because of mapping to 255 range.. 127. But in the case of free running mode, along with 127, 191, 63 only are observed. And it can also be seen that nearly 127 = 63*2, and 191 = 63*3. And when current waveform exists, it's highly distorted, but perceptible that it's not the previous signal of just 127, 191, 63. In the below plot, current is passed since 33.5sec.

I am unable to understand this, structured noise. What may be the reason ? I have once read, last two MSB bits get noisy at this sampling rate, but then how would 127, jump to 191. Why is 63 getting added, subtracted at each instant ?
I understand the reason for non-uniform sampling now is because of Serial.print(), whose interrupts are obstructing the ADC interrupts, but don't understand the reason behind the noise observed.
Following is the code uploaded in arduino. Serial data is read from matlab by fread() function.
long t1=0,ts1=0,ts2=0;
int aval=0, val=0;

void setup() {

 TIMSK0 = 0x00;           // disable timer (causes anoying interrupts)
 DIDR0 = 0x3F;            // digital inputs disabled
 ADMUX = 0x40;            // measuring on ADC0, right adjust, AREF reference
 ADCSRA = 0xAC;           // AD-converter on, interrupt enabled, prescaler = 16
 ADCSRB = 0x00;           // AD channels MUX on, free running mode
 bitWrite(ADCSRA, 6, 1);  // Start the conversion by setting bit 6 (=ADSC) in ADCSRA
 sei(); 
 Serial.begin(115200); 
 t1=millis(); 
 }

ISR(ADC_vect)
{
 aval = ADCL;    
 aval += ADCH << 8; 
} 

void loop() {
val = map(aval, 0, 1023, 0, 255);
Serial.write(val);
delay(10);
}


user7508: It is very unclear what you are trying to achieve here. You are clocking
the ADC at its maximum frequency, which has it take one reading every
13 µs, and yet you are delaying for 10 ms after sending one
reading through the serial port. So basically your Matlab program is
getting one reading out of every 769 you take.
If you want fast data acquisition with a consistent sampling frequency,
you basically have two options:

clock the ADC as fast as you dare, buffer the data in RAM and, when
the buffer is full, send it to the host computer
set a high baud rate and take the data as fast as the UART can send
it.

The first option allows for faster sampling rates, but only as short
bursts, as limited by the available RAM. Given what you have already
written I assume you want continuous acquisition, and thus the second
option. I also assume you are using an Arduino Uno or similar.
At 115.2 kb/s (actually 117.647 kb/s given the rounding), every bit sent
takes 136 CPU cycles, or 8.5 µs. One byte takes 10 bits
(start bit + 8 data bits + stop bit), i.e. 85 µs.
This means there is no point in clocking your ADC that fast: you can set
the prescaler to 64 and each reading will take 52 µs.
What I would do here is configure the ADC to be triggered by
Timer 1, and have that timer deliver the trigger signal every
1360 CPU cycles exactly. Then,
ISR(ADC_vect)
{
    Serial.write(ADCH);
}

assuming of course you have set the left-adjust mode (ADLAR bit in
ADMUX).
With the ADC and the UART working at the very same frequency, you keep
the UART busy without ever filling the output buffer. If your ADC runs
faster than the UART, the buffer will fill out and your timings will get
nasty. On the other hand, if you are really confident in your timings,
you could skip the Serial managing of that buffer and just do
UDR0 = ADCH in the ADC ISR.

user13432: If you use an ISR to retrieve the ADC result, you will need to protect the read of the variable. An interrupt may occur when reading aval in the loop(), as this is a 16-bit value and the read requires several instructions. This may give corrupt values. 
void loop() {
  noInterrupts();
    val = aval;
  interrupts();
  val = map(val, 0, 1023, 0, 255);
  Serial.write(val);
  delay(10);
}

Cheers!

1711: <p>To make an oscilloscope in matlab environment, for data acquisition on arduino side, when inbuilt <code>analogRead()</code> is used, it's working fine, but with very low sampling rate, that aliasing is clearly observed. That too, non-uniform sampling. Thus listing out the drawbacks: </p>

<ol>
<li>Very slow data rate, even if, sent with 115200 baudrate,</li>
<li>Aliasing</li>
<li>Non-uniform sampling</li>
</ol>

<p><a href="https://i.stack.imgur.com/kuBUf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/kuBUf.png" alt="Observed waveform, analogRead()"></a></p>

<p>Thus, to overcome above, ADC in free running mode was used, with 16 as prescaling factor, thus 76.9KHz sampling rate (counting each ADC as 13 clock cycles). Thus, data is uniformly sampled now. Also it is quite fast, because of sampling, and is observed. The only problem now exists is, output has a specific noise. The data being sampled is 50Hz sinusoidal current waveform by current sensor ACS712. It overlays instantaneous current waveform over a bias of 2.5V. Thus, when signal is not present, a constant voltage of 2.5V is observed, thus, 512, because of mapping to 255 range.. 127. But in the case of free running mode, along with 127, 191, 63 only are observed. And it can also be seen that nearly 127 = 63*2, and 191 = 63*3. And when current waveform exists, it's highly distorted, but perceptible that it's not the previous signal of just 127, 191, 63. In the below plot, current is passed since 33.5sec.
<a href="https://i.stack.imgur.com/4wkzE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4wkzE.png" alt="Observed waveform, free running mode"></a></p>

<p>I am unable to understand this, structured noise. What may be the reason ? I have once read, last two MSB bits get noisy at this sampling rate, but then how would 127, jump to 191. Why is 63 getting added, subtracted at each instant ?
I understand the reason for non-uniform sampling now is because of <code>Serial.print()</code>, whose interrupts are obstructing the ADC interrupts, but don't understand the reason behind the noise observed.</p>

<p>Following is the code uploaded in arduino. Serial data is read from matlab by <code>fread()</code> function.</p>

<pre><code>long t1=0,ts1=0,ts2=0;
int aval=0, val=0;

void setup() {

 TIMSK0 = 0x00;           // disable timer (causes anoying interrupts)
 DIDR0 = 0x3F;            // digital inputs disabled
 ADMUX = 0x40;            // measuring on ADC0, right adjust, AREF reference
 ADCSRA = 0xAC;           // AD-converter on, interrupt enabled, prescaler = 16
 ADCSRB = 0x00;           // AD channels MUX on, free running mode
 bitWrite(ADCSRA, 6, 1);  // Start the conversion by setting bit 6 (=ADSC) in ADCSRA
 sei(); 
 Serial.begin(115200); 
 t1=millis(); 
 }

ISR(ADC_vect)
{
 aval = ADCL;    
 aval += ADCH &lt;&lt; 8; 
} 

void loop() {
val = map(aval, 0, 1023, 0, 255);
Serial.write(val);
delay(10);
}
</code></pre>
 7508: <p>It is very unclear what you are trying to achieve here. You are clocking
the ADC at its maximum frequency, which has it take one reading every
13&nbsp;µs, and yet you are delaying for 10&nbsp;ms after sending one
reading through the serial port. So basically your Matlab program is
getting one reading out of every 769 you take.</p>

<p>If you want fast data acquisition with a consistent sampling frequency,
you basically have two options:</p>

<ul>
<li>clock the ADC as fast as you dare, buffer the data in RAM and, when
the buffer is full, send it to the host computer</li>
<li>set a high baud rate and take the data as fast as the UART can send
it.</li>
</ul>

<p>The first option allows for faster sampling rates, but only as short
bursts, as limited by the available RAM. Given what you have already
written I assume you want continuous acquisition, and thus the second
option. I also assume you are using an Arduino Uno or similar.</p>

<p>At 115.2 kb/s (actually 117.647 kb/s given the rounding), every bit sent
takes 136&nbsp;CPU cycles, or 8.5&nbsp;µs. One byte takes 10&nbsp;bits
(start bit +&nbsp;8&nbsp;data bits +&nbsp;stop bit), i.e. 85&nbsp;µs.
This means there is no point in clocking your ADC that fast: you can set
the prescaler to&nbsp;64 and each reading will take 52&nbsp;µs.</p>

<p>What I would do here is configure the ADC to be triggered by
Timer&nbsp;1, and have that timer deliver the trigger signal every
1360&nbsp;CPU cycles <em>exactly</em>. Then,</p>

<pre class="lang-c++ prettyprint-override"><code>ISR(ADC_vect)
{
    Serial.write(ADCH);
}
</code></pre>

<p>assuming of course you have set the left-adjust mode (<code>ADLAR</code> bit in
<code>ADMUX</code>).</p>

<p>With the ADC and the UART working at the very same frequency, you keep
the UART busy without ever filling the output buffer. If your ADC runs
faster than the UART, the buffer will fill out and your timings will get
nasty. On the other hand, if you are really confident in your timings,
you could skip the <code>Serial</code> managing of that buffer and just do
<code>UDR0 = ADCH</code> in the ADC ISR.</p>
 13432: <p>If you use an ISR to retrieve the ADC result, you will need to protect the read of the variable. An interrupt may occur when reading <code>aval</code> in the <code>loop()</code>, as this is a 16-bit value and the read requires several instructions. This may give corrupt values. </p>

<pre><code>void loop() {
  noInterrupts();
    val = aval;
  interrupts();
  val = map(val, 0, 1023, 0, 255);
  Serial.write(val);
  delay(10);
}
</code></pre>

<p>Cheers!</p>
