View previous topic :: View next topic |
Author |
Message |
Leo22 Guest
|
Peak detector |
Posted: Mon Apr 02, 2007 4:24 pm |
|
|
I did this code for a sine peak detector , but it's not so precise . Does some have any idea how can I improve it to get more precison?And another question , I'm just put a diode for the negative cicle of the sine wave , is only that going to protect the Pic16f877 againt negative inputs. And the peak it's lower than 5V. Thanks
void main()
{
Long int cont=0;
float value,value1,value2, peak=0,peak1=0;
set_tris_d(0);
set_tris_c(0);
set_tris_b(0b01000000);
output_d(0);
output_c(0);
LCD_init();
setup_adc_ports(RA0_RA1_RA3_ANALOG);
setup_adc(ADC_CLOCK_DIV_32);
setup_timer_1(T1_INTERNAL | T1_DIV_BY_1);
set_adc_channel (0);
delay_us(10);
while (cont<250>peak)
peak=value;
value2=read_adc();
if (value1>peak)
peak=value1;
if (value2>peak)
peak=value2;
cont++;
}
peak= (5*(peak+1)/1023);
} |
|
|
kender
Joined: 09 Aug 2004 Posts: 768 Location: Silicon Valley
|
Re: Peak detector |
Posted: Mon Apr 02, 2007 7:26 pm |
|
|
Leo22 wrote: | Does some have any idea how can I improve it to get more precison? |
You know that the waveform coming into the A/D is a half-wave rectified sine. Integrate over one half-period (instead of looking for a peak mesurement). Knowing the frequency of the sine and the integral, you can calculate the peak. |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Tue Apr 03, 2007 8:19 am |
|
|
What are you going to use this peak for? If you know the signal is a really good sine then kender's idea will give a very good answer. But if the further the signal differs from a true sign the more error.
If you are measuring AC voltage or power you probably want RMS not peak. You can calculate RMS from kender's integral.
If you want the peak value so you can adjust gain to prevent clipping then you want the real peak whether it is a sine or not.
To protect the A/D from the negative half cycle a resistor and a Schottky diode work well. A common signal diode (1N914) will work up to audio frequencies. Don't use 1N4000 series diodes above about 400 Hz. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
Leo22 Guest
|
|
Posted: Tue Apr 03, 2007 4:03 pm |
|
|
I have to mesuare a gain between to sine waves, but to mesuare the RMS value I need a external circuit, I want to put the sine wave directly to the PIC. |
|
|
languer
Joined: 09 Jan 2004 Posts: 144 Location: USA
|
|
Posted: Wed Apr 04, 2007 2:08 am |
|
|
I think you need to define your requirements a little better and then look at the underlying math.
- Are you detecting fixed frequency waveforms?
- Are the waveforms same frequency, different amplitude?
The easiest way, for a fixed frequency system is a true peak detector (i.e. series diode, shunt cap). Anything more complex than this (or using the MCU for the sampling/peak detection/smoothing) will be very waveform dependent (unless you can achieve tru-RMS performance). |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Wed Apr 04, 2007 11:01 am |
|
|
Leo22 wrote: | I have to mesuare a gain between to sine waves, but to mesuare the RMS value I need a external circuit, I want to put the sine wave directly to the PIC. |
RMS is just the Root of the Mean Square, all doable in PIC software, no hardware required. What is the frequency of the sine waves? _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
Leo22 Guest
|
|
Posted: Wed Apr 04, 2007 1:31 pm |
|
|
It's 1khz , can I get the rms value with Pic? How can I do that? Thanks for your attention |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Wed Apr 04, 2007 2:33 pm |
|
|
It is the Root of the Mean of the Square. So you take each datapoint, square it, average it over some period of time like one cycle, and take the square root. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
Rocket
Joined: 29 Aug 2005 Posts: 27
|
|
Posted: Thu Apr 05, 2007 2:45 pm |
|
|
Have a look at 'EX_RMSDB.C' in the Examples directory.
SHALOM! _________________ J.I.L. |
|
|
languer
Joined: 09 Jan 2004 Posts: 144 Location: USA
|
|
Posted: Sun Apr 08, 2007 2:33 am |
|
|
One thing you may want to keep in mind is that for either peak detection or RMS to actually work you must sample the signal using the Nyquist criteria. On the analog domain this is not really a problem, but on the digital domain you must sample the signal at least twice as fast as the maximum frequency. For 1kHz you are probably ok, but nonetheless you should verify it. |
|
|
|