Filter signal via mean median filter

Hello,

i want to filter a signal with an mean median filter, can anyone help me? (Without class or object)

Hello

Take a search engine of your choice and ask the WWW for am item.

It is very simple to write a median filter, and it has been done several times for Arduino.

For additional help, you will need to provide the details of your particular problem.

I have search for a filter and i found this one:

>  struct meanDiffFilter_t
> > {
> >   float values[MEAN_FILTER_SIZE];
> >   byte index;
> >   float sum;
> >   byte count;
> > };
> > 
> > float MAX30100::meanDiff(float M, meanDiffFilter_t* filterValues)
> > {
> >   float avg = 0;
> > 
> >   filterValues->sum -= filterValues->values[filterValues->index];
> >   filterValues->values[filterValues->index] = M;
> >   filterValues->sum += filterValues->values[filterValues->index];
> > 
> >   filterValues->index++;
> >   filterValues->index = filterValues->index % MEAN_FILTER_SIZE;
> > 
> >   if(filterValues->count < MEAN_FILTER_SIZE)
> >     filterValues->count++;
> > 
> >   avg = filterValues->sum / filterValues->count;
> >   return avg - M;
> > }

But the filter use struct and i never worked with it, i will read about it, but maybe someone can help me with the same filter and a "simple" Arduino/C++ Code?

i've always had to implement a sort algorithm

output

disp
    807
    249
     73
    658
    930
    272
    544
    878
    923
    709
    440
    165
    492
     42
    987
    503
    327
    729
    840
    612
median
    249
    249
    658
    658
    544
    544
    878
    878
    709
    440
    440
    165
    492
    503
    503
    503
    729

char s [80];

// -----------------------------------------------------------------------------
void
sort (
    int *buf,
    int  N )
{
 // printf ("%s:\n", __func__);
    int t;
    for (int j = N-1; j > 0; j--) {
        for (int i = 0; i < j; i++)  {
            if (buf [i] > buf [i+1])  {
                t         = buf [i];
                buf [i]   = buf [i+1];
                buf [i+1] = t;
            }
            // disp(buf, N, sprintf(" %d %d", j, i))
        }
    }
}

// -------------------------------------
void
disp (
    int *buf,
    int  size )
{
    Serial.println (__func__);
    for (int n = 0; n < size; n++)  {
        sprintf (s, " %6d", *buf++);
        Serial.println (s);
    }
}

// -------------------------------------
void
fill (
    int *buf,
    int  size )
{
    Serial.println (__func__);
    for (int n = 0; n < size; n++)
        *buf++ = random (1000);
}

// -------------------------------------
void
median (
    int *buf,
    int  size,
    int  Nfilt )
{
    Serial.println (__func__);

    int tmp [20];
    for (int n = 0; n < size - Nfilt; n++)  {
        memcpy (tmp, &buf [n], Nfilt * sizeof(int));
        sort (tmp, Nfilt);
        sprintf (s, " %6d", tmp [Nfilt/2]);
        Serial.println (s);
    }
}

// -----------------------------------------------------------------------------
#define N 20
int  buf [N];

void
setup (void)
{
    Serial.begin (9600);

    fill (buf, N);
    disp (buf, N);

#if 0
    sort (buf, N);
    disp (buf, N);
#endif
    median (buf, N, 3);
}

void
loop (void)
{
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.