xyiii 0 Posted March 16, 2015 Share Posted March 16, 2015 (edited) I have a sinusoidal wave of 50Hz, this is the code that I used to read adc. for(i = 0;i<20;i++)//chosen 20 values { ADCValue = analogRead(analogInPin); delay(20); sample[i] = ADCValue;} the results were correct with sine wave display at correct value, however, the signal is at 50Hz, hence this delay(20) is at the time period of each cycle of this waveform, Why am I getting the correct adc results rather than a constant value of each time it reads at the same point? Please can someone answer, thank you so much! Edited March 16, 2015 by bluehash [Admin] Please use code tags while posting code. Quote Link to post Share on other sites
bluehash 1,581 Posted March 16, 2015 Share Posted March 16, 2015 I have a sinusoidal wave of 50Hz, this is the code that I used to read adc. for(i = 0;i<20;i++)//chosen 20 values { ADCValue = analogRead(analogInPin); delay(20); sample[i] = ADCValue;} the results were correct with sine wave display at correct value, however, the signal is at 50Hz, hence this delay(20) is at the time period of each cycle of this waveform, Why am I getting the correct adc results rather than a constant value of each time it reads at the same point? Please can someone answer, thank you so much! Not sure what you are trying to do. Are you saying that you are not getting constant values? You will get values that are maybe close in value, but not constant. If you are sampling a 50Hz signal, you should be sampling at least 2 times faster - Nyquist frequency. Quote Link to post Share on other sites
xyiii 0 Posted March 16, 2015 Author Share Posted March 16, 2015 Not sure what you are trying to do. Are you saying that you are not getting constant values? You will get values that are maybe close in value, but not constant. If you are sampling a 50Hz signal, you should be sampling at least 2 times faster - Nyquist frequency. Thank you so much for your reply, I thought my value was wrong but it is actually correctly sampling and gives the same original signal. I am not sure how this delay(20) works, and the sampling frequency in this seemed not to be 20ms. Quote Link to post Share on other sites
enl 227 Posted March 17, 2015 Share Posted March 17, 2015 In addition to undersampling the signal leading to loss of proper representation, the clock on the processor is NOT as accurate or as precise as a crystal oscillator. If the timing in your code was nominally perfect, it would still likely not match the real-world waveform timing, and not read at exactly the same point on the real world waveform each time, even if the real world wave had no error, which is unlikely. Undersampling IS used for a number of practical applications, but great care is needed to do it properly. Another issue, that again would affect you even with perfect clock, is that the delay(20) puts a 20 microsecond delay BETWEEN the instructions before it and after it. The 20microsec does not account for a) the time required to do the function calls and ADC conversion, the time to save the value to the variable ADCvalue, c) the time to copy the value into the array element, d) the loop overhead. Depending on the processor clock speed, the overhead will be a couple microseconds at 16MHz to maybe 100 microsec or more at 1MHz. xyiii, dubnet and bluehash 3 Quote Link to post Share on other sites
xyiii 0 Posted March 19, 2015 Author Share Posted March 19, 2015 In addition to undersampling the signal leading to loss of proper representation, the clock on the processor is NOT as accurate or as precise as a crystal oscillator. If the timing in your code was nominally perfect, it would still likely not match the real-world waveform timing, and not read at exactly the same point on the real world waveform each time, even if the real world wave had no error, which is unlikely. Undersampling IS used for a number of practical applications, but great care is needed to do it properly. Another issue, that again would affect you even with perfect clock, is that the delay(20) puts a 20 microsecond delay BETWEEN the instructions before it and after it. The 20microsec does not account for a) the time required to do the function calls and ADC conversion, the time to save the value to the variable ADCvalue, c) the time to copy the value into the array element, d) the loop overhead. Depending on the processor clock speed, the overhead will be a couple microseconds at 16MHz to maybe 100 microsec or more at 1MHz. Dear Sir, Thank you so much for your time, If my signal is at 50Hz in order to not under sampling the signal, what time should I delay in this function? Since if I put delay 10, it will not be a perfect sine wave but it gives a interference of 2 sine waves, delay 1 would reach the results faster but made no difference with delay 10. If this is the case, how do I do it properly can you please give some suggestions? Quote Link to post Share on other sites
enl 227 Posted March 20, 2015 Share Posted March 20, 2015 As @@bluehash said, to sample he input so as to get sufficient information to reproduce it, the sample rate needs to be greater than twice the maximum frequency in the signal. First, I messed up units in my post above. I intermingles 50 microseconds and 50 milliseconds. The concept still applies and content is correct otherwise... Second, we really need to know what you are trying to do for anyone to give a better response. Are you trying to synchronise to the input? Sample it like a digital oscillloscope? other? What development tools are you using (I presume from your post that the toolset you are using implements delay() as a millisecond delay, not microsecond)? What clock rate is your processor running at (if using Energia, this is probably 16MHz+/- a few percent. If CCS, unless you change it, it is the processor default of 1MHz uncalibrated.) What is YOUR background (so we can go to the appropriate level of explanation) Short form: Nyquist demonstrated that to gain sufficient information about a sampled signal to reproduce it without aliasing (shifting frequency components to the wrong frequency), the sample rate must be GREATER than twice the maximum frequency present in the input. (It is often stated that it must be EQUAL OR GREATER, but equal fails, as the sampled data then becomes phase dependent for the highest frequencies). It is better to sample at a minimum of 4 to 10 times the max frequency, as this reduces the need to interpolate and filter to get information, such as peak, power, frequency spectrum, and phase, and reduces the time required to gather enough samples to get this information (as there will be much less modulation due to interference between the sample frequency and the signal than if the sample rate is barely greater than the max frequency in the signal) The BEST way to do sampling is a stable clock (crystal controlled) and a sampling system with no jitter. This can be approximated on the MSP430 quite well by using interrupts and the hardware timers to control the process. In brief, you set a timer to interrupt at your sample rate, and the interrupt handler starts the next ADC cycle, giving a fairly well synchronized sample point. Having the ADC interrupt at the end of the conversion allows immediate handling of the result. This is not simple, but it isn't that complicated, either. To tell you more, we need to know more about what you are trying to do. bluehash and xyiii 2 Quote Link to post Share on other sites
xyiii 0 Posted March 20, 2015 Author Share Posted March 20, 2015 As @@bluehash said, to sample he input so as to get sufficient information to reproduce it, the sample rate needs to be greater than twice the maximum frequency in the signal. First, I messed up units in my post above. I intermingles 50 microseconds and 50 milliseconds. The concept still applies and content is correct otherwise... Second, we really need to know what you are trying to do for anyone to give a better response. Are you trying to synchronise to the input? Sample it like a digital oscillloscope? other? What development tools are you using (I presume from your post that the toolset you are using implements delay() as a millisecond delay, not microsecond)? What clock rate is your processor running at (if using Energia, this is probably 16MHz+/- a few percent. If CCS, unless you change it, it is the processor default of 1MHz uncalibrated.) What is YOUR background (so we can go to the appropriate level of explanation) Short form: Nyquist demonstrated that to gain sufficient information about a sampled signal to reproduce it without aliasing (shifting frequency components to the wrong frequency), the sample rate must be GREATER than twice the maximum frequency present in the input. (It is often stated that it must be EQUAL OR GREATER, but equal fails, as the sampled data then becomes phase dependent for the highest frequencies). It is better to sample at a minimum of 4 to 10 times the max frequency, as this reduces the need to interpolate and filter to get information, such as peak, power, frequency spectrum, and phase, and reduces the time required to gather enough samples to get this information (as there will be much less modulation due to interference between the sample frequency and the signal than if the sample rate is barely greater than the max frequency in the signal) The BEST way to do sampling is a stable clock (crystal controlled) and a sampling system with no jitter. This can be approximated on the MSP430 quite well by using interrupts and the hardware timers to control the process. In brief, you set a timer to interrupt at your sample rate, and the interrupt handler starts the next ADC cycle, giving a fairly well synchronized sample point. Having the ADC interrupt at the end of the conversion allows immediate handling of the result. This is not simple, but it isn't that complicated, either. To tell you more, we need to know more about what you are trying to do. I am trying to read the value from 50Hz sine wave and calculate its DC offset and its rms voltage. Hence, I am reading the adc value where it produced a sine wave exact as the input wave though with some deviation. I am using energia, and wanting to reproduce the signal fully. I thought I am getting 20 samples in a cycle of the 50Hz waveform which looks like it is on excel by plotting those adc results but this delay(20) seemed wrong in terms of sampling frequency theorem. I don't understand why am I doing at a wrong delay value but still getting a reproduced sine wave, and now I seemed to have some idea why I get a not precise reproduced sine wave. Thank you very much for your reply, Can you please give me some advice in terms of reading it more accurately? I am using the energy ardunio language and not really familiar with C in terms of stable clock etc Thank you so much. Quote Link to post Share on other sites
enl 227 Posted March 20, 2015 Share Posted March 20, 2015 First, theory: The wikipedia page on aliasing isn't bad. ANY sample rate that is not a sub-multiple of your sine wave frequency (without considering phase) will give you data that looks like a sine wave at some frequency or another. The RMS and offset will be the same as the wave you are sampling, but the frequency will not. The original wave can be reconstructed from undersampled data in some cases IF there is other information available. This is how many oscilloscopes used to handle very high frequencies, and some still do, using low speed D/A converters. The TIMING of the samples on a 1ns scope is held to less than 1ns jitter, but the samples might be done very precisely at 500ns or more apart, timed at successively larger delays (1ns more per trigger) from the (repeating) trigger event. For a repetitive waveform, the waveform can be reproduced from samples spread over a large number of repetitions, as long as the max frequency is less than the 1ns (and a few other conditions are met.) Essentially, this is what you are doing, but in a free running way. If you know it is a stable sine wave, then all you need is the upper and lower peak to get RMS and offset. It does not matter which cycle you measure the peaks on. If you are trying to do true-RMS calculation, then you need more data, but, again, if the signal is stable and repetitive, it doesn't matter if all samples come from the same cycle. In fact, for these tasks, clock jitter and frequency aren't critical either. If you were trying to reproduce the waveform, not analyze the basic properties, these would matter. xyiii 1 Quote Link to post Share on other sites
xyiii 0 Posted March 20, 2015 Author Share Posted March 20, 2015 First, theory: The wikipedia page on aliasing isn't bad. ANY sample rate that is not a sub-multiple of your sine wave frequency (without considering phase) will give you data that looks like a sine wave at some frequency or another. The RMS and offset will be the same as the wave you are sampling, but the frequency will not. The original wave can be reconstructed from undersampled data in some cases IF there is other information available. This is how many oscilloscopes used to handle very high frequencies, and some still do, using low speed D/A converters. The TIMING of the samples on a 1ns scope is held to less than 1ns jitter, but the samples might be done very precisely at 500ns or more apart, timed at successively larger delays (1ns more per trigger) from the (repeating) trigger event. For a repetitive waveform, the waveform can be reproduced from samples spread over a large number of repetitions, as long as the max frequency is less than the 1ns (and a few other conditions are met.) Essentially, this is what you are doing, but in a free running way. If you know it is a stable sine wave, then all you need is the upper and lower peak to get RMS and offset. It does not matter which cycle you measure the peaks on. If you are trying to do true-RMS calculation, then you need more data, but, again, if the signal is stable and repetitive, it doesn't matter if all samples come from the same cycle. In fact, for these tasks, clock jitter and frequency aren't critical either. If you were trying to reproduce the waveform, not analyze the basic properties, these would matter. Thank you so much sir, I now understand that fully, However when I am trying to read rms value by square them, sum them up, divided by number of samples to find the mean, and square root them, it is not always printing a stable number. However, if i divide the sum by (number of samples - 1) then it gives a more stable value. Do you have any suggestion on that? Really appreciated your time, and sorry for disturbing. Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.