https://github.com/torvalds/linux
Revision 5885b9b3f030f9b430f6b1c7fa396c885033f2f8 authored by Linus Torvalds on 16 December 2011, 19:14:42 UTC, committed by Linus Torvalds on 16 December 2011, 19:14:42 UTC
* 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tiwai/sound:
  ALSA: HDA: Use LPIB Position fix for Intel SCH Poulsbo
  ALSA: hda: fix mute led polarity for HP laptops with buggy BIOS
  ALSA: HDA: Set position fix to LPIB for an Atom/Poulsbo based device
  ASoC: Fix hx4700 error handling to free gpios if snd_soc_register_card fails
  ASoC: WM8958: correctly show firmware magic on mismatch
  ASoC: mxs: Add appropriate MODULE_ALIAS()
  ASoC: mxs: Add missing MODULE_LICENSE("GPL")
  ASoC: Fix WM8996 24.576MHz clock operation
  ASoC: Include linux/module.h for smdk2443_wm9710
  ASoC: Fix a typo in jive_wm8750
  ASoC: Fix build dependency for SND_SOC_JZ4740_CODEC
  ASoC: Include linux/io.h for jz4740 codec
2 parent s 1c5ff0f + 844ec31
Raw File
Tip revision: 5885b9b3f030f9b430f6b1c7fa396c885033f2f8 authored by Linus Torvalds on 16 December 2011, 19:14:42 UTC
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tiwai/sound
Tip revision: 5885b9b
average.c
/*
 * lib/average.c
 *
 * This source code is licensed under the GNU General Public License,
 * Version 2.  See the file COPYING for more details.
 */

#include <linux/module.h>
#include <linux/average.h>
#include <linux/bug.h>
#include <linux/log2.h>

/**
 * DOC: Exponentially Weighted Moving Average (EWMA)
 *
 * These are generic functions for calculating Exponentially Weighted Moving
 * Averages (EWMA). We keep a structure with the EWMA parameters and a scaled
 * up internal representation of the average value to prevent rounding errors.
 * The factor for scaling up and the exponential weight (or decay rate) have to
 * be specified thru the init fuction. The structure should not be accessed
 * directly but only thru the helper functions.
 */

/**
 * ewma_init() - Initialize EWMA parameters
 * @avg: Average structure
 * @factor: Factor to use for the scaled up internal value. The maximum value
 *	of averages can be ULONG_MAX/(factor*weight). For performance reasons
 *	factor has to be a power of 2.
 * @weight: Exponential weight, or decay rate. This defines how fast the
 *	influence of older values decreases. For performance reasons weight has
 *	to be a power of 2.
 *
 * Initialize the EWMA parameters for a given struct ewma @avg.
 */
void ewma_init(struct ewma *avg, unsigned long factor, unsigned long weight)
{
	WARN_ON(!is_power_of_2(weight) || !is_power_of_2(factor));

	avg->weight = ilog2(weight);
	avg->factor = ilog2(factor);
	avg->internal = 0;
}
EXPORT_SYMBOL(ewma_init);

/**
 * ewma_add() - Exponentially weighted moving average (EWMA)
 * @avg: Average structure
 * @val: Current value
 *
 * Add a sample to the average.
 */
struct ewma *ewma_add(struct ewma *avg, unsigned long val)
{
	avg->internal = avg->internal  ?
		(((avg->internal << avg->weight) - avg->internal) +
			(val << avg->factor)) >> avg->weight :
		(val << avg->factor);
	return avg;
}
EXPORT_SYMBOL(ewma_add);
back to top