Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Signal period and DC offset by ADC

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR
View previous topic :: View next topic  
Author Message
cucuruzel

Bascom Member



Joined: 18 Mar 2010
Posts: 53
Location: Cluj-Napoca

romania.gif
PostPosted: Fri Mar 08, 2019 12:12 am    Post subject: Signal period and DC offset by ADC Reply with quote

Hi

I need to measure a triangular signal with a 10bit ADC and display the DC offset and Frequency (period).
The singnal is mostly symetrical, 5 to 100Hz, aprox. 2.5V DC offset and DC deviation needs to be measured.

I have made a simple simulator in Autoit to test the code that would be inside a 1ms timer interrupt, results in the screen capture.



This is some code (full script is attached, Autoit 3.3.14.2)

Code:
    For $i = 1 to $sim_samples

    ;----- signal generator -----
;               $adc=$gen_ampl*ASin(sin($i/$gen_period))+$gen_offset                                                                                                    ;TRIANGLE
                $adc=($gen_ampl/$gen_period) * ($gen_period - ABS(MOD($i, (2*$gen_period)) -$gen_period) )+$gen_offset  ;TRIANGLE2
;               $adc=($gen_ampl*sin(2*$pi*($i/100)))+$gen_offset                                                                                                                ;SINE

                $adc=$adc+Random(-$noise_ampl,$noise_ampl,1)    ;add random noise over signal

    ;----- signal capture an proccessing -----
        ;this would be in a 1ms timer interrupt (1Ks/s)
                $sig_period = $sig_period +1                                    ;count period ticks
                if $adc>$sig_max then $sig_max = $adc                   ;max peak detector
                if $adc<$sig_min then $sig_min = $adc                   ;min peak detector
                $sig_offset = (($sig_max-$sig_min)/2)+$sig_min  ;signal DC offset

                if $adc> $sig_offset then                                               ;detect "rising" signal
                        if $sig_dir=0 then                                                      ;signal was "falling" before
                                $sig_max=$adc                                                   ;reset maximum
                                $sig_avg_offset = ($sig_avg_offset + $sig_offset)/2     ;average DC offset
                                if $sig_trans_cnt= 2 then                               ;second transition, full period
                                        $avg_smin=($avg_smin+$sig_min)/2        ;average minimum
                                        $sig_avg_period=($sig_avg_period+$sig_period)/2         ;average signal period
                                        ConsoleWrite(@crlf & "! instant period : " & $sig_period & "  avg: " & $sig_avg_period)
                                        $sig_period=0                                           ;reset period counter
                                        $sig_trans_cnt=1                                        ;reset transition counter
                                        _GraphGDIPlus_Refresh($Graph)
                                endif
                        endif
                        $sig_dir = 1                                                            ;signal is "rising"
                endif
                if $adc< $sig_offset then                                               ;signal is "falling"
                        if $sig_dir=1 then                                                      ;signal was "raising" before
                                $sig_min=$adc                                                   ;reset minimum
                                $sig_avg_offset = ($sig_avg_offset + $sig_offset)/2 ;average DC offset
                                if $sig_trans_cnt= 1 then                               ;first transition
                                        $sig_trans_cnt=2                                        ;next transition
                                        $avg_smax=($avg_smax+$sig_max)/2        ;average maximum
                                        _GraphGDIPlus_Refresh($Graph)
                                endif
                        endif
                        $sig_dir = 0                                                            ;signal is "falling"
                endif
    ;----- draw graph -----

 


In short a min/max peak detector is implemented, the offset= ((max-min)/2 )+min, signal transitions are detected when signal intersects the offset and by counting two transitions in same direction a period is detected.

Is there a better way of doing this while keeping it low resource/simple ?

(BASCOM-AVR version : 2.0.8.1 )
Back to top
View user's profile Visit poster's website Yahoo Messenger
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum