Numeric Filter Synthesis : CutOff Frequency 50Hz (Or 60Hz) - Page 1
Forum section: electronics / general

author: ThomasT
author_rank: Newbie
timestamp: February 26, 2025, 08:09:16 am
content: Good morning,I encounter the following problem (As part of my job).Using a microcontroller, I digitalize a signal comming from a microphone at a sampling frequency of 16KHz.(This signal goes through a harware preamplifier). At the output, I get a 250 samples buffer.An algorithm computes statistics on this buffer, but this one is disturbed by the presence of a 50Hz sinus (Comming from the 50Hz power supply) mixed to the noise recorded by the microphone.I would like to implement a numeric filter on the buffer in order to suppress the 50Hz frequency (Or 60Hz depending on the country).I studied numeric filtering 15 years ago, I remember that we managed to synthesis FIR filter using Matlab (Choosing a window, the filter order and so on...) and I remember that we managed to get filtercoefficients which was implemented on a DSP.Now, I do not remember the method, I do not remember how to obtain these coefficients and I do not rememeber how to implement these kind of filter on a microcontroller...Does anyone know how to do it ? Does anyone can explain me the method in details and how to obtain the coefficients of the filter ?Moreover, does anyone know how to implement this kind of filter on a microcontroller practically please ?If anyone has good knowledges on numeric filtering, thank you by advance for your help on this subject.I wish you a good day !ThomasT

author: gf
author_rank: Super Contributor
timestamp: February 26, 2025, 10:06:34 am
content: 250 samples @16kSa/s arevery fewfor a decent 50Hz or 60Hz notch filter. What bandwidht do you have in mind for the notch filter? Less than roghly 400Hz (double sided BW, which means that the notch filter becomes de facto a highpass with ~200Hz cut-off) is hardly possibly with such a low number of samples, and even with such a large BW, the filter's settling time will "eat" 50% of the samples, so that only say 100...150 samples will be left for your calculation. Is it possible to acquire significantly more samples?Another question is what statistics you want to calculate on the samples? You may not be happy if the filter removes mains hum, but garbles the statistical properties you want to measure. And what kind of signal do you want to measure? Does it have useful frequency content below (say) 200Hz?

author: JPortici
author_rank: Super Contributor
timestamp: February 26, 2025, 10:45:17 am
content: Quote from: gf on February 26, 2025, 10:06:34 amthe filtering might be happening before the 250 samples are gathered.-> acquire data continously-> run through filter continously-> 250 samples starting from moment "X", picked from filtered data.Then the FIR can be arbitrarly long, or maybe cascading multiple IIR might be feasible if processing power/memory is scarce or the FIR would introduce too much group delay

author: tggzzz
author_rank: Super Contributor
timestamp: February 26, 2025, 10:45:50 am
content: Be aware that you might need to track the mains frequency; it is 49.5Hz to 50.5Hz in the UK, not 50Hz.Similarly, mains interference isn't a sine wave. Not only does the mains have significant harmonics nowadays, but capacitive coupling will emphasise disturbances that are synchronous with the mains.Consider using an n-path filter (DSP or AnalogueSP). They can have arbitrarily high Q, very steep skirts, and also remove the 50Hz harmonics.

author: gf
author_rank: Super Contributor
timestamp: February 26, 2025, 11:15:12 am
content: Quote from: JPortici on February 26, 2025, 10:45:17 amSure. Unfortunately, the OP is not clear on whether a continuous stream of samples (without gaps) is acquired, or whether the are gaps between the acquired buffers (so that each buffer must be considered a separate measurement window).

author: JPortici
author_rank: Super Contributor
timestamp: February 26, 2025, 11:33:09 am
content: Quote from: gf on February 26, 2025, 11:15:12 amAh, of course! that was me assuming the OP could

author: radiolistener
author_rank: Super Contributor
timestamp: February 26, 2025, 11:01:05 pm
content: Code:Fs = 16000; % Sampling frequency (Hz)F0 = 50; % Notch frequency (Hz)Q = 30; % Quality factor% Compute second-order filter coefficientsw0 = 2 * pi * F0 / Fs;alpha = sin(w0) / (2 * Q);b = [1, -2 * cos(w0), 1];a = [1 + alpha, -2 * cos(w0), 1 - alpha];% Normalize coefficientsb = b / a(1);a = a / a(1);% Print coefficientsfprintf('b: [');fprintf('%g, ', b);fprintf(']\n');fprintf('a: [');fprintf('%g, ', a);fprintf(']\n');% Plot frequency responsefreqz(b, a, 2^20, Fs);Code:b: [0.999673, -1.99896, 0.999673, ]a: [1, -1.99896, 0.999346, ]Code:N = 2^24;signal = 2 * randi([0, 1], 1, N) - 1; % digital noiseresult = filter(b, a, signal);Code:N = 2^16;signal = 1.0 * cos(50 * 2*pi*(0:N-1)/Fs) + ...0.25 * cos(300 * 2*pi*(0:N-1)/Fs); % two-tone signal 50 Hz + 300 Hzresult = filter(b, a, signal);