Home Search Forums Register Forum Rules Man Pages Unix Commands Linux Commands FAQ Members Search Today's Posts Mark Forums Read

 UNIX for Dummies Questions & Answers This forum is closed for new posts. Please post beginner questions to learn unix and learn linux in this forum UNIX for Beginners Questions & Answers

# "Smoothing" the data

## UNIX for Dummies Questions & Answers

#1
02-07-2012   -   Original Discussion by cosmologist
 cosmologist Registered User Join Date: Apr 2009 Last Activity: 28 June 2013, 3:19 PM EDT Posts: 173 Thanks: 39 Thanked 0 Times in 0 Posts
"Smoothing" the data

Hi!

Is there a way to use awk to "smooth" a set of data? That is, doing what the "SMOOTH" function in IDL does (SMOOTH) ?

Thanks!
#2
02-07-2012   -   Original Discussion by cosmologist
 Corona688   Mead Rotor Join Date: Aug 2005 Last Activity: 17 November 2017, 9:25 PM EST Location: Saskatchewan Posts: 22,512 Thanks: 1,151 Thanked 4,273 Times in 3,946 Posts
Surely there's a way to do that with awk. awk doesn't have a single function for it of course, since it doesn't make much sense to do a boxcar average on a single line...

Let me see if I have the algorithm right. For these points:

Code:
```1
5
2
6
3
7
4
8
5
9```

for a boxcar average of width 3, would you want:

Code:
`(1+5+2)/3, (5+2+6)/3, (2+6+3)/3, (6+3+7)/3, (3+7+4)/3, (7+4+8)/3, (4+5+5)/3, (8+5+9)/3`

---------- Post updated at 04:22 PM ---------- Previous update was at 04:10 PM ----------

Code:
```\$ cat data
1
5
2
6
3
7
4
8
5
9

\$ cat smooth.awk

BEGIN { WIDTH=3 }

{       DATA[(++N)%WIDTH]=\$1    }

(N>=WIDTH) {
V=0
for(X=(N+1); X<=(N+WIDTH); X++)
V+=DATA[X%WIDTH];

print V/WIDTH;
}

\$ awk -f smooth.awk data
2.66667
4.33333
3.66667
5.33333
4.66667
6.33333
5.66667
7.33333
4.66667

\$```

Last edited by Corona688; 02-07-2012 at 06:20 PM..
 The Following User Says Thank You to Corona688 For This Useful Post: cosmologist (02-13-2012)
#3
02-20-2012   -   Original Discussion by cosmologist
 cosmologist Registered User Join Date: Apr 2009 Last Activity: 28 June 2013, 3:19 PM EDT Posts: 173 Thanks: 39 Thanked 0 Times in 0 Posts
Quote:
 Originally Posted by Corona688 Surely there's a way to do that with awk. \$ cat smooth.awk BEGIN { WIDTH=3 } { DATA[(++N)%WIDTH]=\$1 } (N>=WIDTH) { V=0 for(X=(N+1); X<=(N+WIDTH); X++) V+=DATA[X%WIDTH]; print V/WIDTH; }
I am trying to apply this idea to "degrade" the output of a model resolution.

For the sample:

\$ vi model.dat

Code:
```3002.000 1.2874e+32
3002.300 1.2898e+32
3002.600 1.2966e+32
3002.900 1.3000e+32
3003.200 1.2997e+32
3003.500 1.2961e+32
3003.800 1.2926e+32
3004.100 1.2909e+32
3004.400 1.2904e+32
3004.700 1.2923e+32
3005.000 1.2970e+32
3005.300 1.3027e+32
3005.600 1.3043e+32
3005.900 1.3028e+32
3006.200 1.2977e+32
3006.500 1.2894e+32
3006.800 1.2802e+32
3007.100 1.2738e+32
3007.400 1.2717e+32
3007.700 1.2728e+32
3008.000 1.2778e+32```

I want to "somehow" degrade the resolution of the first column from 0.3 to 3 for the corresponding numbers in the second column something like this:

Code:
```3002.000 ???
3005.000 ???
3008.000 ???```

Where the "???" are the numbers I am trying to obtain using the awk script that you suggested but I am not able to get the results . Any suggestions?
#4
02-21-2012   -   Original Discussion by cosmologist
 cosmologist Registered User Join Date: Apr 2009 Last Activity: 28 June 2013, 3:19 PM EDT Posts: 173 Thanks: 39 Thanked 0 Times in 0 Posts
Any thoughts on this... I am still not able to solve it
#5
02-21-2012   -   Original Discussion by cosmologist
 Corona688   Mead Rotor Join Date: Aug 2005 Last Activity: 17 November 2017, 9:25 PM EST Location: Saskatchewan Posts: 22,512 Thanks: 1,151 Thanked 4,273 Times in 3,946 Posts
It took you a week to get back to me. It can't be urgent enough for you to break the rules by bumping.

What is the ??? supposed to be? How would I arrive at it? In what way does the code I gave you "not work"? My last two posts were one big question, "do I have the right algorithm", and you never answered me.
#6
02-21-2012   -   Original Discussion by cosmologist
 cosmologist Registered User Join Date: Apr 2009 Last Activity: 28 June 2013, 3:19 PM EDT Posts: 173 Thanks: 39 Thanked 0 Times in 0 Posts
I am sorry I didn't mean to sound rude, the reason I took a week is that I was trying to figure it out myself first
It is not urgent but I just thought that if more details are needed for my question I can give more details..
I am sorry for that... the second post wasn't only for you, it was for anyone who might have an idea... I am really sorry
#7
02-21-2012   -   Original Discussion by cosmologist
 Corona688   Mead Rotor Join Date: Aug 2005 Last Activity: 17 November 2017, 9:25 PM EST Location: Saskatchewan Posts: 22,512 Thanks: 1,151 Thanked 4,273 Times in 3,946 Posts