From: Martin Peres <martin.peres@linux.intel.com>
To: Simon Ser <simon.ser@intel.com>, igt-dev@lists.freedesktop.org
Cc: martin.peres@intel.com
Subject: Re: [igt-dev] [PATCH i-g-t v2 1/3] lib/igt_audio: detect noise and pops
Date: Tue, 4 Jun 2019 15:52:50 +0300 [thread overview]
Message-ID: <19a8ce76-4ba2-bad8-4cde-1873dfc6e54e@linux.intel.com> (raw)
In-Reply-To: <20190604114836.2830-2-simon.ser@intel.com>
On 04/06/2019 14:48, Simon Ser wrote:
> First, normalize the bin power by dividing it by the number of input samples.
> We need to multiply by 2 since we get half as many bins as input samples.
>
> Second, check that low frequencies are under a given threshold. If there is a
> pop or some noise, the low frequencies will be affected.
Reviewed-by: Martin Peres <martin.peres@linux.intel.com>
>
> Signed-off-by: Simon Ser <simon.ser@intel.com>
> ---
> lib/igt_audio.c | 58 ++++++++++++++++++++++++++++++++++++++++++++-----
> 1 file changed, 53 insertions(+), 5 deletions(-)
>
> diff --git a/lib/igt_audio.c b/lib/igt_audio.c
> index 08c0fb6af0db..e0b1bafe1be2 100644
> --- a/lib/igt_audio.c
> +++ b/lib/igt_audio.c
> @@ -39,6 +39,17 @@
> #define CHANNELS_MAX 8
> #define SYNTHESIZE_AMPLITUDE 0.9
> #define SYNTHESIZE_ACCURACY 0.2
> +/** MIN_FREQ: minimum frequency that audio_signal can generate.
> + *
> + * To make sure the audio signal doesn't contain noise, #audio_signal_detect
> + * checks that low frequencies have a power lower than #NOISE_THRESHOLD.
> + * However if too-low frequencies are generated, noise detection can fail.
> + *
> + * This value should be at least 100Hz plus one bin. Best is not to change this
> + * value.
> + */
> +#define MIN_FREQ 200 /* Hz */
> +#define NOISE_THRESHOLD 0.0005
>
> /**
> * SECTION:igt_audio
> @@ -108,6 +119,7 @@ int audio_signal_add_frequency(struct audio_signal *signal, int frequency,
>
> igt_assert(index < FREQS_MAX);
> igt_assert(channel < signal->channels);
> + igt_assert(frequency >= MIN_FREQ);
>
> /* Stay within the Nyquist–Shannon sampling theorem. */
> if (frequency > signal->sampling_rate / 2) {
> @@ -304,6 +316,12 @@ void audio_signal_fill(struct audio_signal *signal, double *buffer,
> audio_sanity_check(buffer, signal->channels * samples);
> }
>
> +/* See https://en.wikipedia.org/wiki/Window_function#Hann_and_Hamming_windows */
> +static double hann_window(double v, size_t i, size_t N)
> +{
> + return v * 0.5 * (1 - cos(2.0 * M_PI * (double) i / (double) N));
> +}
> +
> /**
> * Checks that frequencies specified in signal, and only those, are included
> * in the input data.
> @@ -328,6 +346,16 @@ bool audio_signal_detect(struct audio_signal *signal, int sampling_rate,
> data = malloc(samples_len * sizeof(double));
> memcpy(data, samples, samples_len * sizeof(double));
>
> + /* Apply a Hann window to the input signal, to reduce frequency leaks
> + * due to the endpoints of the signal being discontinuous.
> + *
> + * For more info:
> + * - https://download.ni.com/evaluation/pxi/Understanding%20FFTs%20and%20Windowing.pdf
> + * - https://en.wikipedia.org/wiki/Window_function
> + */
> + for (i = 0; i < data_len; i++)
> + data[i] = hann_window(data[i], i, data_len);
> +
> /* Allowed error in Hz due to FFT step */
> freq_accuracy = sampling_rate / data_len;
> igt_debug("Allowed freq. error: %d Hz\n", freq_accuracy);
> @@ -338,8 +366,7 @@ bool audio_signal_detect(struct audio_signal *signal, int sampling_rate,
> igt_assert(0);
> }
>
> - /* Compute the power received by every bin of the FFT, and record the
> - * maximum power received as a way to normalize all the others.
> + /* Compute the power received by every bin of the FFT.
> *
> * For i < data_len / 2, the real part of the i-th term is stored at
> * data[i] and its imaginary part is stored at data[data_len - i].
> @@ -349,15 +376,36 @@ bool audio_signal_detect(struct audio_signal *signal, int sampling_rate,
> * The power is encoded as the magnitude of the complex number and the
> * phase is encoded as its angle.
> */
> - max = 0;
> bin_power[0] = data[0];
> for (i = 1; i < bin_power_len - 1; i++) {
> bin_power[i] = hypot(data[i], data[data_len - i]);
> - if (bin_power[i] > max)
> - max = bin_power[i];
> }
> bin_power[bin_power_len - 1] = data[data_len / 2];
>
> + /* Normalize the power */
> + for (i = 0; i < bin_power_len; i++)
> + bin_power[i] = 2 * bin_power[i] / data_len;
> +
> + /* Detect noise with a threshold on the power of low frequencies */
> + for (i = 0; i < bin_power_len; i++) {
> + freq = sampling_rate * i / data_len;
> + if (freq > MIN_FREQ - 100)
> + break;
> + if (bin_power[i] > NOISE_THRESHOLD) {
> + igt_debug("Noise level too high: freq=%d power=%f\n",
> + freq, bin_power[i]);
> + return false;
> + }
> + }
> +
> + /* Record the maximum power received as a way to normalize all the
> + * others. */
> + max = NAN;
> + for (i = 0; i < bin_power_len; i++) {
> + if (isnan(max) || bin_power[i] > max)
> + max = bin_power[i];
> + }
> +
> for (i = 0; i < signal->freqs_count; i++)
> detected[i] = false;
>
>
_______________________________________________
igt-dev mailing list
igt-dev@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/igt-dev
next prev parent reply other threads:[~2019-06-04 12:52 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-06-04 11:48 [igt-dev] [PATCH i-g-t v2 0/3] Check for audio noise and pops Simon Ser
2019-06-04 11:48 ` [igt-dev] [PATCH i-g-t v2 1/3] lib/igt_audio: detect " Simon Ser
2019-06-04 12:52 ` Martin Peres [this message]
2019-06-04 11:48 ` [igt-dev] [PATCH i-g-t v2 2/3] lib/tests/igt_audio: add a pop test Simon Ser
2019-06-04 12:54 ` Martin Peres
2019-06-05 8:03 ` [igt-dev] [PATCH i-g-t v3] lib/tests/igt_audio: add a test holding a sample Simon Ser
2019-06-12 10:56 ` Martin Peres
2019-06-04 11:48 ` [igt-dev] [PATCH i-g-t v2 3/3] lib/tests/igt_audio: add phaseshift detection test Simon Ser
2019-06-04 13:01 ` Martin Peres
2019-06-04 12:23 ` [igt-dev] ✓ Fi.CI.BAT: success for Check for audio noise and pops Patchwork
2019-06-04 23:39 ` [igt-dev] ✓ Fi.CI.IGT: " Patchwork
2019-06-05 10:48 ` [igt-dev] ✓ Fi.CI.BAT: success for Check for audio noise and pops (rev2) Patchwork
2019-06-06 14:00 ` [igt-dev] ✓ Fi.CI.IGT: " Patchwork
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=19a8ce76-4ba2-bad8-4cde-1873dfc6e54e@linux.intel.com \
--to=martin.peres@linux.intel.com \
--cc=igt-dev@lists.freedesktop.org \
--cc=martin.peres@intel.com \
--cc=simon.ser@intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox