# Sampling, filters, windowing, FFT. From theory to help on this coding list

My plan is to analyse the spectrum of samples from a microphone.

I wonder how correct this suggestion is. The below description may then fail on several points. I am in need of somebody with a red pencil. This is a perfect hoppy project for a retired programmer. But it's a little too much for me. The list below is partly done by help I got at [1].

7 PDM mics are sampled at 3.072 MHz and brought down to 48 kHz in a library. The decimators also contain filters. I simply use one of the mics.

I need the 48 kHz for the headset output, which runs on a PLL that only seems to make sense at this frequency.

I then resample by just picking out every third. I get to 16 kHz. I pick out 1024 values, over 64 ms.

Prior to this I have an anti aliasing filter F1 from 48 kHz to 8 kHz, Nyquist for 16 kHz. It's a second order biquad IIR filter, direct form I, as in [2]. Params are as in [3], part LPF. Params are f0/Fs = 8/48 = 0.1667 and Q = 0.707 (1(sqrt(2)).

Then I do bit reverse and forward FFT and a split spectrum.

I end up with two 0..4kHz spectra in 256 values, giving 15.625 Hz/value. Each spectrum covers 32 ms.

Prior to the bit reverse I have an anti aliasing filter F2 from 16 kHz to 4 kHz, Nyquist for 8 kHz. Same type as F1. Params are f0/Fs = 4/16 = 0.25 and Q = 0.707 (1(sqrt(2)).

At the moment no windowing before the FFT.

I have a figure about this in [4]. I have done some measurements with a scope using a tone generator. It may be garbage-in, garbage out [5]. Both these are also mentioned in the last comments in [1].

Now, is the theory correct, the params and the degree of the filters? I could cascade the biquad filters to get 4th, 6th order filter. Anything else that's problematic and need to be corrected?

[2] Digital biquad filter - Wikipedia

[4] Signal flow

• What's not clear is your goal. What is the result you want to see in the end and how do you measure "good enough" (requirements). What is the maximum bandwidth of the signal, what is the maximum allowable distortion? Do you have a processing time requirement? What are the features in the final waveform that you are trying to extract? Also not sure what F1 (below) and F2 (below). I'll add an answer specific to the 16 KHz decimation processing. May 12, 2022 at 13:18
• I am detecting alarms of different kinds, to help a person with weak hearing. A bandwidth of 4 kHz is needed for the older type fire alarms. Distorsion could be high (but it's not, I think). I am using a board from XMOS with 7 mics and an xCORE-200 processor with two "tiles" and 8 "logical cores" on each. They are all independent of each other. It's all over-documented in My Beep-BRRR notes. I have removed the two confusing headings, they just pointed to the somewhere below paragraphs. May 12, 2022 at 13:39
• It would be good to break this into multiple individual questions following the site guidelines (consider how long just answering the first part of your question is!). We can't really provide complete design services, but answering the specific questions as to where you are stuck is game! May 12, 2022 at 14:15
• Can you explain what your scope pictures are? What are the different traces supposed to be and how are they different from what you expect? May 12, 2022 at 15:26
• Ok, I admit it's long! Even too long! Thanks for the advice! The scope pictures are 3.5 kHz and 4.5 kHz from an online signal generator, picked up by the mic. Plus the filters running as shown in the other figure. The 4.5 kHz value has wrapped down to the place of the 3.5 kHz, since the FFT takes me to 4.00 kHz. It seems that these mirror values are are smaller with anti-aliasing filters, F1 is most effective. but F1 and F2 best. However, with no filters it looks like the mirror 4.5 kHz value is somewhat larger than the 3.5 kHz value! But these data may be garbage, both in and out. May 13, 2022 at 13:43

There is a lot of detail in the question and I am not sure of all the requirements and desired results that would affect the processing after the 16 KHz decimated samples are produced. However I can provide details and comments up to that point that may be helpful.

To answer first, there is nothing fundamentally incorrect in the approach the OP is taking: ensuring any aliasing bands are filtered out prior to down-sampling and this filtering is done in the case using biquad IIR filters. The use of biquads could be an advantage if delay is of upmost concern, otherwise I would change this to a linear phase multiband filter designed with the least squares algorithm. Both can be made to work and the important points detailed below are to understand where the rejection is needed and how much.

Decimation is just a form of sampling, and all considerations and requirements for aliasing apply, so in the interest of understanding decimation, I start with an understanding of aliasing due to the sampling process.

The following post demonstrates how without a band-selection filter, the analog signal when sampled at 10 Hz, could have been an 11 Hz sine wave or a 1 Hz sinewave. If both were present (and neither filtered out prior to sampling) the resulting sampled waveform would be the interference result of the two.

Sampling and the creation of multiple images at integer multiples of the sampling frequency

Further insights with an analogy to frequency mixing (for those familiar with that analog process): Higher order harmonics during sampling

Below shows the spectrums for a typical sampling process of a low pass continuous-time waveform. The top graph is the continuous-time waveform with spectral content limited to less than half the sampling rate. The middle graph is the spectrum of the sampling process: when "sampling", we multiply the time domain waveform with an impulse train spaced at the sampling period. The Fourier Transform of an impulse train in time is an impulse train in frequency, spaced by the sampling rate. Multiplication in time is convolution in frequency, and thus we get the bottom graph as the resulting discrete time waveform. Here, the frequency axis is extended to plus or minus infinity, but due the periodicity, we only need to show the discrete-time waveform's spectrum from $$-f_s/2$$ to $$+f_s/2$$ since it repeats periodically everywhere else. For purposes of understanding resampling operations and to cohesively work with combined analog and digital signal processing, I find it convenient to consider the "unrolled digital spectrum" extending to $$\pm \infty$$ as I have done here.

To note, if the continuous time waveform had spectral content limited to $$f_s \pm f_s/2$$ as I have depicted below (and yes if the waveform is complex, then it can have spectrum in the positive frequency axis alone- however getting into complex signal processing is not important to this answer), the exact same discrete time waveform will result!

The important item to see here is from where aliasing occurs specifically in the frequency domain, and how to optimize filtering to prevent it. The plot below shows all the frequency zones in red that would result in irreversible aliasing (without other information about the signal): any spectral content within these red zones would alias into the same fundamental blue zone in our discrete-time spectrum representing our waveform of interest (in this case the low pass waveform in the continuous-time domain that is in blue). Signal anywhere else is of less of a concern since it would alias to be outside of our frequency band of interest, and therefore we have opportunity to filter it later, downstream with very efficient and effective digital filtering. Anything that lands in band however is problematic.

This suggests an optimum filter would concentrate it's rejection at these alias bands specifically. In the continuous-time domain, it is often simpler to implement a low pass filter but additional nulls could be added at these frequency locations to improve rejection if needed. However, in the discrete-time domain, such multiband filters are easily synthesized and commonly used for resampling applications.

What is to be noted from the plots above, that the decimation approximation and the aliasing mechanism is equivalent! Below shows the same considerations when going from the higher sampling rate for 48 KHz and resampling to 16 KHz. The purple lines on the second graph show the passband and rejection bands for the optimized anti-alias filter (the filter like the other spectrums is periodic so it typically defined in the range of $$\pm fs/2$$ where we see there is a defined passband and define rejection band. The rest of the regions are "don't care"

Such multiband filters for resampling applications are easily designed as linear phase FIR filters using the least-squares algorithm, provided optimum rejection and passband performance (in a least-squares error sense) for a given filter complexity. Such filters are designed using Matlab, Octave and Python scipy.signal using the firls function. The amount of rejection required is based on distortion requirements and amount of signal present (including quantization noise) in each of the alias bands.

• Thanks! In XMOS lib_dsp the design-in-place only covers biquads and cascading them. Wouldn't that be ok for anti-aliasing? May 12, 2022 at 14:29
• @ØyvindTeig Yes that would be fine for anti-aliasing. Do you have the tools and know-how to create and evaluate the frequency response versus target requirements? May 12, 2022 at 15:38
• 41+ years in the industry plus actually working with this kind of problem area for some of those years (radar based fluid level measurement system, Autronica GL-90 - in the eighties!) After that it was mostly real-time concurrent safety-critical sw. The DSP knowledge is rusty, but I'm willing! Plus, I occasionally come across some DSP-close Master's here, censoring at NTNU - which I would learn from.. I have a good scope etc. I think I should also make a sw-based sweep if necessary. I already conditionally compile a 480 Hz insted of the 48 kHz. (I guess we're soon routed to chat..) May 12, 2022 at 16:55
• I could potentially also use/do some Python, but I have tried to stear away - since this is not actually the project as such. I have a chapter on tools in that blog note - like here - where I also fired up Python. May 12, 2022 at 16:57
• @ØyvindTeig it’s more detail then we can go through here but I teach an online course starting next week covering all this including examples in Python. You can check it out under the course tab at DSPRelated.com - it would be perfect for you May 12, 2022 at 17:03