DSPRelated.com
Forums

Simple filter in C

Started by andrewstanfordjason 5 months ago6 replieslatest reply 5 months ago329 views

I'm coding up an implementation of a filter and to test it I would like a set of coefficients. Does anyone know how to make filter coefficients such that:

  • it can implemented in a small amount of C
  • the coefficients are 32 bits, say Q31 format
  • the gain of the filter is not greater than 1 and not less than 1/1024
  • the length of the filter is variable
  • the filter is not symmetric (or even close to being symmetric)

The obvious choice is rand() x filter length but I don't love it. Thanks for any suggestions.

[ - ]
Reply by paulob160July 17, 2024

Good Morning, can I suggest you root around in Numerix-DSP's Siglib library ? It was open-sourced some time ago and is chock-full of DSP algorithms written in ANSI C. BR Paul

[ - ]
Reply by LKoppJuly 17, 2024

Hello,

Why not trying something like a sweep with various bandwidth and central frequency. The result is intuitive and easy to test..Regards. Laurent

[ - ]
Reply by CharlieRaderJuly 17, 2024

There's a lot I don't understand about your question. Let me first say that, even if I did understand it, I wouldn't be able to help, because it has been decades since I used the C language to create a program.


The first issue is "What do you mean by a filter?"  I'm guessing that you mean a digital filter and that would mean that there is an input which is a sequence of numbers, there is an output which is another sequence of numbers, and there is a specified linear process that can be executed to convert input to output.  But you haven't said whether the filter input is a finite length sequence, whether the entire input sequence is in the system storage at the same time, whether the process is supposed to go on forever, etc. You haven't said whether the number coding for the input and the number coding for the output are the same. 

You don't say whether the input is to be an arbitrary input, or an input provided after the filter has been specified. Is it samples from a measurement? Is it samples comnputed by some other process, like a  random number generator, or a specified frequency sinewave, or a random sequence drawn from a specified spectral density, or something else.


Next, you talk about the gain of the filter. But most people would consider the gain as a function of frequency, some people would limit the meaning of the gain at any specific frequency to a magnitude gain, others to a magnitude and phrase.

From your other conditions I think you are using the term "filter" to mean a list of numbers which is the finite length impulse response of a non-recursive filter.  Since you say that is non-symmetric, it means the filter gain will have a non-trivial phase response.


You give us no clue about what it is that you want to test? Do you want to test whether your program correctly runs the linear relationship? Do you want to know how long it takes per sample? Do you want to identify quantization effects? 

Your last line, about an obvious choice of rand(x), makes it even harder to know what you need.




[ - ]
Reply by andrewstanfordjasonJuly 17, 2024

You are correct, I left a lot out, sorry. Here are some more details:

  • The filter is an digital FIR
  • as for the 'gain' I mean that the absolute value of the output should be between 1 and 1/1024 times the magnitude of the input.
  • the actual coefficients are unimportant other than the requirements I outlined. I was just asking to see if there was some neat trick for creating a set of FIR filter coefficients with a small amount of C.

Thanks

[ - ]
Reply by emebJuly 17, 2024
[ - ]
Reply by andrewstanfordjasonJuly 17, 2024
cheers