目录
介绍
背景
使用代码
介绍音频处理器通常使用定点数字表示音频信号。在开发用于连接到音频处理器的GUI的过程中,我需要一种简单的方法来向用户显示定点编号。这个简单的类是一个定点数容器,它允许将值读取或写入为定点值或double。
背景定点数历史悠久,尤其是在浮点模块可用于CPU之前。DSP通常会使用固定数字格式,因为它们通常没有浮点模块。
音频DSP以定点数字格式表示音频信号。“1.31”的音频格式有一个数字范围或者-1...... +1。在我的特定例子中,我需要使用数字范围为-8... +8 的“4.20”格式。“4.20”格式将具有1个符号位,3个十进制位和20个小数位。
使用代码FixedPoint类介绍如下:
///
/// A class to convert fixed point numbers from/to doubles.
///
public class FixedPoint
{
private readonly int _fracWidth;
private readonly int _decWidth;
private readonly int _fracMask;
private readonly int _decMask;
private readonly int _signMask;
private readonly int _fullMask;
private readonly double _minValue;
private readonly double _maxValue;
#region Properties
private readonly bool _error;
public bool Error
{
get { return _error; }
}
///
/// Is value negative
///
public bool IsNegative
{
get { return (ValueAsFixedPoint & _signMask) != 0; }
}
///
/// Get decimal part of fixed number
///
public int GetDecimal
{
get { return (ValueAsFixedPoint >> _fracWidth) & _decMask; }
}
///
/// Get fraction part of fixed point number
///
public int GetFraction
{
get { return ValueAsFixedPoint & _fracMask; }
}
private int _val;
///
/// Get/Set value with fixed point number
///
public int ValueAsFixedPoint
{
get { return _val; }
set { _val = value & _fullMask; }
}
///
/// Get/Set value with double
///
public double ValueAsDouble
{
get { return ConvertToDouble(_val); }
set { _val = ConvertToFixedPoint(value); }
}
#endregion
///
/// Instantiate a fixed point number
///
/// fixed point number format
public FixedPoint(string format)
{
// extract pieces from the format definition
var s = format.Split(new char[] { '.' });
if (s.Length != 2) { _error = true; return; }
var b = int.TryParse(s[0], out _decWidth);
if (!b) { _error = true; return; }
b = int.TryParse(s[1], out _fracWidth);
if (!b) { _error = true; return; }
// calculate values to be used later
for (var i = 0; i < _fracWidth; ++i) _fracMask = (_fracMask
关注
打赏
最近更新
- 深拷贝和浅拷贝的区别(重点)
- 【Vue】走进Vue框架世界
- 【云服务器】项目部署—搭建网站—vue电商后台管理系统
- 【React介绍】 一文带你深入React
- 【React】React组件实例的三大属性之state,props,refs(你学废了吗)
- 【脚手架VueCLI】从零开始,创建一个VUE项目
- 【React】深入理解React组件生命周期----图文详解(含代码)
- 【React】DOM的Diffing算法是什么?以及DOM中key的作用----经典面试题
- 【React】1_使用React脚手架创建项目步骤--------详解(含项目结构说明)
- 【React】2_如何使用react脚手架写一个简单的页面?
立即登录/注册


微信扫码登录