[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [PROGRAMMERING] Simplet hack til glidende gennemsnit



On Tue, 16 Mar 2004, Klaus Friis Østergaard wrote:

> Er der en simple vej til at lave et glidene gennemsnit via f. eks
> sed & awk

Med awk, det er der sikkert.

> Jeg har en masse temperatur data:
>  
> 1 20.01
> 2 20.05
> 3 20.03
> 4 20.06
> 5 20.07
> 6 20.05
> ....
>  
> (1+2+3+4+5)/5  
> (2+3+4+5+6)/5
> ect...
>  
> kan man snildt lave det i sed og awk

Hvad med sh og dc?

Hvis dine data ser ud som ovenstående, så:
#!/bin/sh
NUMSAMP=5
RADIX=3
SUM=0
IDX=0
SAMPLES=()
while read NUM VALUE; do
        if [ ${#SAMPLES[*]} -eq $NUMSAMP ]; then
                SUM=$(dc -e "$SUM ${SAMPLES[$IDX]} - p")
        fi
        SAMPLES[$IDX]=$VALUE
        SUM=$(dc -e "$SUM $VALUE + p")
        echo -n "Temp $NUM = $VALUE : "
        echo $(dc -e "$RADIX k $SUM ${#SAMPLES[*]} / p")
        (( IDX=(IDX+1)%NUMSAMP ))
done

Kør script:
  cat data | glide.sh
eller
  glide.sh < data

Bortset fra dc, er det meste beskrevet her:
http://www.linuxbog.dk/unix/unix/shell-script.html

Shell kan ikke håndtere float point, derfor dc.

/hans
-- 
Hamletsgade 4 - 201, DK-2200 København N, Phone: +45 3582 9079
Schou Industries ApS      http://schou.dk/    CVR: 26 13 44 39
--------------------------------------------------------------
Unix: You probably don't need our advice.
			-- FAQ om Flac, www.livemetallica.com


 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 22:43 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *