"Smoothing" the data | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

"Smoothing" the data

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-07-2012
cosmologist cosmologist is offline
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!
Sponsored Links
    #2  
Old 02-07-2012
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 19 September 2014, 8:06 PM EDT
Location: Saskatchewan
Posts: 19,390
Thanks: 790
Thanked 3,270 Times in 3,065 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 05:20 PM..
The Following User Says Thank You to Corona688 For This Useful Post:
cosmologist (02-13-2012)
Sponsored Links
    #3  
Old 02-20-2012
cosmologist cosmologist is offline
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 View Post
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;
}
Thanks for your help
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  
Old 02-21-2012
cosmologist cosmologist is offline
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
Sponsored Links
    #5  
Old 02-21-2012
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 19 September 2014, 8:06 PM EDT
Location: Saskatchewan
Posts: 19,390
Thanks: 790
Thanked 3,270 Times in 3,065 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.

We're not on call, if nobody answers your post immediately, wait!

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.
Sponsored Links
    #6  
Old 02-21-2012
cosmologist cosmologist is offline
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
Sponsored Links
    #7  
Old 02-21-2012
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 19 September 2014, 8:06 PM EDT
Location: Saskatchewan
Posts: 19,390
Thanks: 790
Thanked 3,270 Times in 3,065 Posts
Don't be sorry, just please answer my questions They'll help anyone else trying to help you too.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
awk command to replace ";" with "|" and ""|" at diferent places in line of file shis100 Shell Programming and Scripting 7 03-16-2011 08:59 AM
Make scipt except from "Y","y" and "yes" to take [Enter] as being "yes" hakermania Shell Programming and Scripting 4 12-11-2010 09:24 AM
ps -ef | grep "string1" "string2" " "string3" steve2216 Shell Programming and Scripting 11 09-03-2010 11:08 AM
Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`" Lokesha UNIX for Dummies Questions & Answers 4 12-20-2007 12:52 AM



All times are GMT -4. The time now is 11:55 PM.