[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Discuss-gnuradio] Moving average and history

From: Martin Braun
Subject: [Discuss-gnuradio] Moving average and history
Date: Mon, 9 Feb 2009 13:43:41 +0100
User-agent: Mutt/1.5.17 (2007-11-01)


I was about to write a block which calculates the moving average over a
whole vector, when I stumbled upon the way the current moving average
blocks handle the history.

Here's the code for the float version:

gr_moving_average_ff::work (int noutput_items,
              gr_vector_const_void_star &input_items,
              gr_vector_void_star &output_items)
  const float *in = (const float *) input_items[0];
  float *out = (float *) output_items[0];

  float sum = 0;
  int num_iter = (noutput_items>d_max_iter) ? d_max_iter :\
  for (int i = 0; i < d_length-1 ; i++) {
    sum += in[i];

  for (int i = 0; i < num_iter; i++) {
    sum += in[i+d_length-1];
    out[i] = sum * d_scale;
    sum -= in[i];

  return num_iter;

Here's what I don't understand: the state for the MA (as for all
filters) is saved implicitly in the history. However, in this special
case, the state is simply one scalar value (saved in 'sum'). The way
this is implemented, that state has to be recalculated every time work()
is called. So, why bother with history() and not simply make 'sum' a
class property? Would that screw up the scheduling?

The reason I ask is if I write a MA block for vectors, using history()
means keeping vector_length x MA_length samples in some buffer and
running as many extra multiplications every time work() is called. Of
course, I'd prefer to solve this the GNU Radio way, but in this case it
would seem a waste of memory and CPU cycles.

Thanks for any insight,

Dipl.-Ing. Martin Braun           Phone: +49-(0)721-608 3790
Institut fuer Nachrichtentechnik  Fax:   +49-(0)721-608 6071
Universitaet Karlsruhe (TH)       http://www.int.uni-karlsruhe.de/

Attachment: unnamed.sig
Description: PGP signature

reply via email to

[Prev in Thread] Current Thread [Next in Thread]