GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 22-Mar-2005, 16:02
hellhammer hellhammer is offline
New Member
 
Join Date: Mar 2004
Posts: 25
hellhammer is on a distinguished road

An alternative to block processing


Hello!

I'm trying to implement, in C++, an averaging filter, which is represented by the following equation:

y[n] = (1 - a)*x[n] + y[n-1]

Where y[n] = current output, y[n-1] = previous output and x[n] = current input.

My implementation of this is as follows:

CPP / C++ / C Code:
/* Define Constants */
const int  knNUM_SAMPLES = 100000;           // Number of input samples, change as necessary

const float kfSamplingFrequency = 48000;
const float kfAveragingTime = 1000;          // Change as necessary
float fAveragedSamples = ( kfSamplingFrequency * kfAveragingTime)/1000;

const float fAvgThreshold = 0;           // To be determined 
float fAveragingConstant;                       

/* Input and Output Arrays */
float afInputSamples[knNUM_SAMPLES];
float afAveragedSamples[knNUM_SAMPLES];

/* Instantiate a cAveragingFilter object */
cAveragingFilter AvgFilterObj;

int main()
{
   AvgFilterObj.Load( knNUM_SAMPLES );

   AvgFilterObj.Process();

   return 0;
}

/* Constructor */
cAveragingFilter::cAveragingFilter() 
{
	fAveragingConstant = (float) exp( -2.15 / ( kfAveragingTime * kfSamplingFrequency ) );   
}

/* Destructor */
cAveragingFilter::~cAveragingFilter() { }

void cAveragingFilter::Load(int knNUM_SAMPLES)
{
	// This will be replaced by the routine that reads in the inputs
	// to the averaging filter.

	for ( int i = 0; i < knNUM_SAMPLES; i++ )
	{
		afInputSamples[i] = 1;
	}
}

void cAveragingFilter::Process()
{
   afAveragedSamples[0]  = afInputSamples[0];
   
   for ( int i = 1; i < knNUM_SAMPLES - 1; i++ )
   {
      afInputSamples[i] = abs(afInputSamples[i]);

	  // Check to see if the input value is higher than the threshold
	  if (afInputSamples[i] < fAvgThreshold)
	  {
	     afAveragedSamples[i] = afAveragedSamples[i-1];
	  }
	  else
	  {
	     afAveragedSamples[i] = ( 1 - fAveragingConstant ) * afInputSamples[i] + fAveragingConstant * afAveragedSamples[i-1];
	  }
   }
}

This works fine when the data is known and is read from a static array or from a file, but my application will require the data to be read from an Analog-to-Digital Converter.

How do I rewrite this program so that it doesn't do block-processing but reads data sample by sample. The program will have to keep the previous sample's output in memory, to calculate the average.

Thanks!
  #2  
Old 22-Mar-2005, 16:38
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by hellhammer
Hello!

I'm trying to implement, in C++, an averaging filter, which is represented by the following equation:

y[n] = (1 - a)*x[n] + y[n-1]

Where y[n] = current output, y[n-1] = previous output and x[n] = current input.

Well, in general the algorithm can be described like this:

Algorithm: Repeat for each new value of x:

1. CurrentValueOfY = (something * CurrentValueOfX) + (somthingelse * OldValueOfY);
2. OldValueOfY = CurrentValueOfY;
3. /* do whatever you need with the current value of y */


Generic code could look something like this:
CPP / C++ / C Code:

  /* intitialize y */

  y = 0;
  oldy = 0;

  for (;;) {

    x = GetNewX();
    y = (1 - a) * x + oldy;
    /* use new value of y for whatever you need */
    oldy = y;

  }

Unless you need the history of input and/or output sigals for other purposes, that's the filter!

Does that help?
 
 

Recent GIDBlogProblems with the Navy (Chiefs) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
how to do basic image processing using MFC yuhui MS Visual C++ / MFC Forum 5 08-Aug-2006 06:43
Having a problem Chuckles Computer Hardware Forum 19 13-Sep-2004 13:17
[CONTEST?]Data Structure Test dsmith C Programming Language 2 06-Jun-2004 16:13
Microsoft to block Trillian from MSN messenger on October 15th jrobbio Computer Software Forum - Windows 1 12-Sep-2003 19:35

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 23:57.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.