replica nfl jerseysreplica nfl jerseyssoccer jerseyreplica nfl jerseys forums.silverfrost.com :: View topic - code to window averaging filter
forums.silverfrost.com Forum Index forums.silverfrost.com
Welcome to the Silverfrost forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

code to window averaging filter

 
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> General
View previous topic :: View next topic  
Author Message
Srabon



Joined: 22 Feb 2011
Posts: 14

PostPosted: Mon Jul 04, 2011 10:05 pm    Post subject: code to window averaging filter Reply with quote

Hi there I have a two-column data files with 8192 rows. Can someone tell me how to average each 8 values as tried below? I managed to average first eight values but do not know how to call the succeeding 8 values. The whole purpose is to remove noise from pressure data by window averaging. Thanks!

Code:
program win_avg_filter
implicit none
integer::n,i
real,dimension(8192)::y
open (unit=3, FILE='ptdata.txt', stat=ierror)
ierror = 0
do i = 1,8192
read (3,*,stat=ierror) ydummy,y(i) 
if (ierror /=0) exit
enddo
close (unit=3)
do i = 1,8
total = total + total(i)
fvalue = total/8.


[/code]
Back to top
View user's profile Send private message
davidb



Joined: 17 Jul 2009
Posts: 560
Location: UK

PostPosted: Thu Jul 07, 2011 10:56 pm    Post subject: Re: code to window averaging filter Reply with quote

Srabon wrote:
Hi there I have a two-column data files with 8192 rows. Can someone tell me how to average each 8 values as tried below? I managed to average first eight values but do not know how to call the succeeding 8 values. The whole purpose is to remove noise from pressure data by window averaging. Thanks!


This is my version. I have assumed the window is always 8 values and cannot move past the ends of the data. The output array (z) therefore
has 8192 - 7 = 8185 elements. I used the sum intrinsic to add up the element values.

Incidently, I have not tried to compile this yet, I just typed it in here. I hope it compiles Laughing

I provide a more optimsed version below.

Code:

program win_avg_filter
   implicit none

   integer, parameter :: WINSIZ = 8
   integer:: i,  ierror
   real ::y(8192),  z(8192-WINSIZ+1), ydummy

   ! Open file (note, strictly you should rewind when you open it)
   open (3, FILE='ptdata.txt', position='rewind', stat=ierror)
   if (ierror /= 0) then
      print *, 'Cannot open file'
      stop
   end if

   ! Read values
   do i = 1, 8192
      read (3, *, stat=ierror) ydummy, y(i) 
      if (ierror /= 0) then
         print *, 'Error or End of file found'
         stop
      end if
   end do

   ! Close file
   close (3)

   ! Do averaging.
   do i=1, size(y)-WINSIZ+1
      ! i is always index to first value in the window
      ! Next line does the averaging
      z(i) = sum(y(i:i+WINSIZ-1)) / real(WINSIZ)

      ! You can also do the averaging like this, but I would encourage
      ! you to become familiar with sum as it is more succint (and
      ! possibly quicker).
      ! z(i) = 0.0
      ! do j=i, i+WINSIZ-1
      !    z(i) = z(i) + y(j)
      ! end do
      ! z(i) = z(i) / real(WINSIZ)
   end do

   ! Print results
   do i=1, size(y)-WINSIZ+1
      print *, z(i)
   end do

end program


Thinking about it some more, the averaging can be made more efficient, since at each step all you do is remove the effect of the first point and add the effect of the next new point. So the averaging loop can be written as follows. The one subtraction and one addition at each step is nearly 4 times faster than the 8 additions in the loop above.

Code:

   ! Do averaging.
   z(1) = sum(y(1:WINSIZ))
   do i=2, size(y)-WINSIZ+1
      z(i) = z(i) - y(i-1) + y(i+WINSIZ-1)
   end do
   ! Convert sums to averages
   z = z / real(WINSIZ)

_________________
Programmer in: Fortran 77/95/2003/2008, C, C++ (& OpenMP), java, Python, Perl
Back to top
View user's profile Send private message
Srabon



Joined: 22 Feb 2011
Posts: 14

PostPosted: Sat Jul 09, 2011 12:55 am    Post subject: Reply with quote

Cheers! mate!

I finally worked out in the following way..
Code:

integer, parameter, dimension(*) :: a = [ 1, 4, 5, ..., 2 ]
integer                          :: i
real, dimension(10)              :: avg

avg = [ (sum(a(i * 4 + 1 : (i + 1) * 4)) / 4., i = 0, 9) ]
print *, avg
end
Back to top
View user's profile Send private message
JohnCampbell



Joined: 16 Feb 2006
Posts: 2615
Location: Sydney

PostPosted: Fri Jul 15, 2011 1:43 am    Post subject: Reply with quote

The following variant of your code works with FTN95

Code:
real, dimension(40)    :: a
integer                :: i
real, dimension(10)    :: avg
real*8 random
external random
!
do i = 1, size(a)
   a(i) = random()
end do
!
do i = 1,10
   avg(i) = sum (a(i*4-3 : i*4)) / 4.
end do
print *, avg
!
avg = (/ (sum(a(i*4-3 : i*4)) / 4., i = 1, 10) /)
print *, avg
end
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> General 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


Powered by phpBB © 2001, 2005 phpBB Group