`
隐形的翅膀
  • 浏览: 504181 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

MuLaw and ALaw

 
阅读更多
The mu-law byte bit arrangement is SEEEMMMM (Sign, Exponent, and Mantissa.)

MuLaw
/// <summary>
        /// Encode one mu-law byte from a 16-bit signed integer. Internal use only.
        /// </summary>
        /// <param name="pcm">A 16-bit signed pcm value</param>
        /// <returns>A mu-law encoded byte</returns>
        private static byte encode(int pcm)
        {
            //Get the sign bit.  Shift it for later use without further modification
			// xxxx xxxx xxxx xxxx  
			// 1000 0000 0000 0000 
			// ---------------------
			// ?000 0000 0000 0000
            int sign = (pcm & 0x8000) >> 8;
            //If the number is negative, make it positive (now it's a magnitude)
            if (sign != 0)
                pcm = -pcm;
            //The magnitude must be less than 32635 to avoid overflow
			//MAX = 32635
            if (pcm > MAX) pcm = MAX;
            //Add 132 to guarantee a 1 in the eight bits after the sign bit
			//132 = 1000 0100
            pcm += BIAS;

            /* Finding the "exponent"
             * Bits:
             * 1 2 3 4 5 6 7 8 9 A B C D E F G
             * S 7 6 5 4 3 2 1 0 . . . . . . .
             * We want to find where the first 1 after the sign bit is.
             * We take the corresponding value from the second row as the exponent value.
             * (i.e. if first 1 at position 7 -> exponent = 2) */
            int exponent = 7;
            //Move to the right and decrement exponent until we hit the 1
			// 0x4000 = 100 0000 0000 0000
			// Sxxx xxxx xxxx xxxx
			//  100 0000 0000 0000
			// ---------------------
			//  ?00 0000 0000 0000
            for (int expMask = 0x4000; (pcm & expMask) == 0; exponent--, expMask >>= 1) { }

            /* The last part - the "mantissa"
             * We need to take the four bits after the 1 we just found.
             * To get it, we shift 0x0f :
             * 1 2 3 4 5 6 7 8 9 A B C D E F G
             * S 0 0 0 0 0 1 . . . . . . . . . (meaning exponent is 2)
             * . . . . . . . . . . . . 1 1 1 1
             * We shift it 5 times for an exponent of two, meaning
             * we will shift our four bits (exponent + 3) bits.
             * For convenience, we will actually just shift the number, then and with 0x0f. */
			 // 0x0f = 1111
            int mantissa = (pcm >> (exponent + 3)) & 0x0f;

            //The mu-law byte bit arrangement is SEEEMMMM (Sign, Exponent, and Mantissa.)
            byte mulaw = (byte)(sign | exponent << 4 | mantissa);

            //Last is to flip the bits
            //~01111111 127
            //----------
            // 10000000 128
            return (byte)~mulaw;
        }


//decode 

/// <summary>
        /// Decode one mu-law byte. For internal use only.
        /// </summary>
        /// <param name="mulaw">The encoded mu-law byte</param>
        /// <returns>A short containing the 16-bit result</returns>
        private static short decode(byte mulaw)
        {
            //Flip all the bits
            mulaw = (byte)~mulaw;

            //Pull out the value of the sign bit
            // xxxx xxxx
            // 1000 0000
            // -----------
            // ?000 0000
            int sign = mulaw & 0x80;
            //Pull out and shift over the value of the exponent

            // xxxx xxxx
            //  111 0000
            // ----------
            //  ??? 0000
            int exponent = (mulaw & 0x70) >> 4;
            //Pull out the four bits of data

            // xxxx xxxx
            //      1111
            // ----------
            //      ????

            int data = mulaw & 0x0f;

            //Add on the implicit fifth bit (we know the four data bits followed a one bit)
            // xxxx xxxx
            //    1 0000
            //----------
            //    x xxxx
            data |= 0x10;

            /* Add a 1 to the end of the data by shifting over and adding one.  Why?
             * Mu-law is not a one-to-one function.  There is a range of values that all
             * map to the same mu-law byte.  Adding a one to the end essentially adds a
             * "half byte", which means that the decoding will return the value in the
             * middle of that range.  Otherwise, the mu-law decoding would always be
             * less than the original data. */
            // x xxxx0
            data <<= 1;
            // x xxxx1
            data += 1;
            /* Shift the five bits to where they need to be: left (exponent + 2) places
             * Why (exponent + 2) ?
             * 1 2 3 4 5 6 7 8 9 A B C D E F G
             * . 7 6 5 4 3 2 1 0 . . . . . . . <-- starting bit (based on exponent)
             * . . . . . . . . . . 1 x x x x 1 <-- our data
             * We need to move the one under the value of the exponent,
             * which means it must move (exponent + 2) times
             */
            data <<= exponent + 2;
            //Remember, we added to the original, so we need to subtract from the final
            data -= MuLawEncoder.BIAS;
            //If the sign bit is 0, the number is positive. Otherwise, negative.
            return (short)(sign == 0 ? data : -data);
        }




ALaw
//The a-law byte bit arrangement is SEEEMMMM (Sign, Exponent, and Mantissa.)
 /// <summary>
        /// Encode one a-law byte from a 16-bit signed integer. Internal use only.
        /// </summary>
        /// <param name="pcm">A 16-bit signed pcm value</param>
        /// <returns>A a-law encoded byte</returns>
        private static byte encode(int pcm)
        {
            //Get the sign bit.  Shift it for later use without further modification
            int sign = (pcm & 0x8000) >> 8;
            //If the number is negative, make it positive (now it's a magnitude)
            if (sign != 0)
                pcm = -pcm;
            //The magnitude must fit in 15 bits to avoid overflow
            if (pcm > MAX) pcm = MAX;

            /* Finding the "exponent"
             * Bits:
             * 1 2 3 4 5 6 7 8 9 A B C D E F G
             * S 7 6 5 4 3 2 1 0 0 0 0 0 0 0 0
             * We want to find where the first 1 after the sign bit is.
             * We take the corresponding value from the second row as the exponent value.
             * (i.e. if first 1 at position 7 -> exponent = 2)
             * The exponent is 0 if the 1 is not found in bits 2 through 8.
             * This means the exponent is 0 even if the "first 1" doesn't exist.
             */
            int exponent = 7;
            //Move to the right and decrement exponent until we hit the 1 or the exponent hits 0
            for (int expMask = 0x4000; (pcm & expMask) == 0 && exponent>0; exponent--, expMask >>= 1) { }

            /* The last part - the "mantissa"
             * We need to take the four bits after the 1 we just found.
             * To get it, we shift 0x0f :
             * 1 2 3 4 5 6 7 8 9 A B C D E F G
             * S 0 0 0 0 0 1 . . . . . . . . . (say that exponent is 2)
             * . . . . . . . . . . . . 1 1 1 1
             * We shift it 5 times for an exponent of two, meaning
             * we will shift our four bits (exponent + 3) bits.
             * For convenience, we will actually just shift the number, then AND with 0x0f. 
             * 
             * NOTE: If the exponent is 0:
             * 1 2 3 4 5 6 7 8 9 A B C D E F G
             * S 0 0 0 0 0 0 0 Z Y X W V U T S (we know nothing about bit 9)
             * . . . . . . . . . . . . 1 1 1 1
             * We want to get ZYXW, which means a shift of 4 instead of 3
             */
            int mantissa = (pcm >> ((exponent == 0) ? 4 : (exponent + 3))) & 0x0f;

            //The a-law byte bit arrangement is SEEEMMMM (Sign, Exponent, and Mantissa.)
            byte alaw = (byte)(sign | exponent << 4 | mantissa);

            //Last is to flip every other bit, and the sign bit (0xD5 = 1101 0101)
            return (byte)(alaw^0xD5);
        }
		
		/// <summary>
        /// Decode one a-law byte. For internal use only.
        /// </summary>
        /// <param name="alaw">The encoded a-law byte</param>
        /// <returns>A short containing the 16-bit result</returns>
        private static short decode(byte alaw)
        {
            //Invert every other bit, and the sign bit (0xD5 = 1101 0101)
            alaw ^= 0xD5;

            //Pull out the value of the sign bit
            int sign = alaw & 0x80;
            //Pull out and shift over the value of the exponent
            int exponent = (alaw & 0x70) >> 4;
            //Pull out the four bits of data
            int data = alaw & 0x0f;

            //Shift the data four bits to the left
            data <<= 4;
            //Add 8 to put the result in the middle of the range (like adding a half)
            data += 8;
            
            //If the exponent is not 0, then we know the four bits followed a 1,
            //and can thus add this implicit 1 with 0x100.
            if (exponent != 0)
                data += 0x100;
            /* Shift the bits to where they need to be: left (exponent - 1) places
             * Why (exponent - 1) ?
             * 1 2 3 4 5 6 7 8 9 A B C D E F G
             * . 7 6 5 4 3 2 1 . . . . . . . . <-- starting bit (based on exponent)
             * . . . . . . . Z x x x x 1 0 0 0 <-- our data (Z is 0 only when exponent is 0)
             * We need to move the one under the value of the exponent,
             * which means it must move (exponent - 1) times
             * It also means shifting is unnecessary if exponent is 0 or 1.
             */
            if (exponent > 1)
                data <<= (exponent - 1);

            return (short)(sign == 0 ? data : -data);
        }
分享到:
评论

相关推荐

    anaopg-hyperlink.rar_Linux/Unix编程_processing

    - "Minvlaw13.m"、"law13.m"、"pinvalaw1.m"、"walaw1.m":这些可能是用MATLAB编写的函数,分别对应不同的音频编码算法,如A-law(ALaw)和μ-law(muLaw),这两种量化方法常用于电话系统中。 - "mulaw.m"、...

    安防视频监控系统视频上云解决方案EasyCVR音频基础知识介绍.docx

    - `ffplay -i test.g711a -f alaw -ac 1 -ar 8000`:播放名为test.g711a的文件,格式为alaw(G.711 A律压缩),单声道,采样率为8kHz。 - `ffplay -i test.g711u -f mulaw -ac 1 -ar 8000`:播放名为test.g711u的...

    本程序使用Matlab调用COMSOL进行二元(电容与相对介电常数)数据的生成.zip

    matlab

    操作系统课程设计基于C++实现的操作系统仿真虚拟页式存储管理项目源代码

    操作系统课程设计基于C++实现的操作系统仿真虚拟页式存储管理项目源代码

    西门子S7-1500与V90伺服在定长切断中的应用及优化 - 工业自动化解决方案

    内容概要:本文详细介绍了西门子S7-1500 PLC与V90伺服在定长切断应用中的具体实现方法和技术要点。首先,文章展示了如何利用TIA Portal V16平台配置工艺对象“CuttingAxis”,并解释了关键参数如每转脉冲数、丝杆导程、减速比等的作用。接着,针对可能出现的问题,如轴运行抖动、控制字状态丢失等进行了深入探讨,并提供了相应的解决措施,比如调整前馈参数、修改报文配置等。此外,还分享了一些提高系统性能的小技巧,例如启用动态限制选项、优化速度前馈与位置环配合等。最后,强调了该方案在包装机械和线材加工领域的广泛应用前景及其优势所在。 适合人群:从事工业自动化相关工作的工程师和技术人员,尤其是那些希望深入了解和掌握西门子PLC与伺服系统集成应用的专业人士。 使用场景及目标:适用于需要实现高精度定长切断操作的企业或项目,旨在帮助用户构建稳定可靠的控制系统,确保生产效率和产品质量。 其他说明:文中不仅涵盖了理论知识讲解,还包括大量实际案例分析以及代码示例,便于读者更好地理解和应用所学内容。同时提醒读者关注硬件配置、软件编程、参数调节等多个方面,以达到最佳效果。

    2023年计算机二级Office选择题考前模拟.docx

    2023年计算机二级Office选择题考前模拟.docx

    CMD仿制PYTHON,自己制作,望通过

    目前可以新建目录,具体自己看代码

    基于SpringBoot网上超市(源码+数据库+万字文档)507

    基于SpringBoot网上超市,系统包含两种角色:用户、管理员,系统分为前台和后台两大模块,主要功能如下: 1 管理员功能实现 商品信息管理 管理员可以通过提交商品名称查询商品,并查看该商品的用户评论信息。 用户管理 管理员通过提交用户名来获取用户资料,对有异常情况的用户信息进行修改,并可以详细查看用户资料。 商品评价管理 管理员审核用户对商品的评价,经过审核的评价才会显示,并可以统计商品评价信息。 已支付订单 管理员查看已支付的订单,并逐个进行订单发货。 2 用户功能实现 商品信息 用户可以收藏、立即购买商品,或对商品进行评价,同时将商品添加到购物车。 购物车 用户可以直接下单购买购物车中的商品,或删除购物车中的商品。 确认下单 用户选择地址,查看支付金额信息,以确认订单之前的所有细节。 已支付订单 用户查看已支付的订单,若对购买商品产生后悔,可以申请退款。 二、项目技术 开发语言:Java 数据库:MySQL 项目管理工具:Maven Web应用服务器:Tomcat 前端技术:Vue、 后端技术:SpringBoot框架 三、运行环境 操作系统:Windows、macOS都可以 JDK版本:JDK1.8以上版本都可以 开发工具:IDEA、Ecplise都可以 数据库: MySQL 5.7/8.0版本均可 Tomcat:7.x、8.x、9.x版本均可 Maven:任意版本都可以

    1_媒工十佳决赛RUNDOWN 2.xlsx

    1_媒工十佳决赛RUNDOWN 2.xlsx

    c#联合opencvsharp开发的视觉源码程序包含模板匹配,找线找圆,预处理等功能全部源码,包含图像显示控件,绘制roi

    c#联合opencvsharp开发的视觉源码程序 包含模板匹配,找线找圆,预处理等功能 全部源码,包含图像显示控件,绘制roi

    基于STC89C52单片机的信号发生器20172086102何雨莉_3.zip

    基于STC89C52单片机的信号发生器20172086102何雨莉_3.zip

    2023年西南大学网络与继续教育学院楼宇自动化作业答案.docx

    2023年西南大学网络与继续教育学院楼宇自动化作业答案.docx

    基于SpringBoot与Vue的智慧养老手表管理系统:家庭树、健康数据监控及权限控制

    内容概要:本文详细介绍了基于SpringBoot和Vue构建的智慧养老手表管理系统的关键技术和实现细节。系统主要由家长和养老院管理员两种角色组成,家长可以通过智能手表实时查看老人的健康数据,而管理员则可以进行更全面的数据管理和权限控制。文中重点讨论了家庭树功能的实现,包括使用MyBatis的动态SQL处理复杂的家庭关系查询,以及前端用Vue的el-tree组件展示家庭树结构。健康数据监控方面,系统利用SpringBoot的@Scheduled定时任务生成日报,并通过ECharts进行数据可视化。此外,还涉及了权限控制、加好友功能、熔断机制等多个方面的技术实现。 适合人群:具有一定编程经验的开发者,尤其是熟悉SpringBoot和Vue框架的工程师。 使用场景及目标:适用于开发类似智慧养老系统的项目,旨在提高老年人健康管理的效率和安全性。目标是通过智能手表实时监测老人健康状况,提供及时的健康报告和预警,同时确保系统的稳定性和安全性。 其他说明:文中提到的技术细节对于理解和实现类似的前后端分离架构非常有帮助,特别是关于MySQL 5.7的使用和优化,以及如何处理第三方设备接口的不稳定问题。

    2023年计算机二级资料.doc

    2023年计算机二级资料.doc

    2023年电大数据结构形成性考核册.doc

    2023年电大数据结构形成性考核册.doc

    软考-网络工程师易错100题 2025年

    网络工程师易错100题 2025年

    2023年电子商务技能竞赛方案.doc

    2023年电子商务技能竞赛方案.doc

    2024年大数据软件项目深度研究分析报告.docx

    2024年大数据软件项目深度研究分析报告.docx

    基于Matlab小波变换与凯伦布尔变换的输电线路单相接地故障双端行波测距方法

    内容概要:本文详细介绍了利用Matlab/Simulink构建输电线路单相接地故障测距模型的方法。首先,通过建立110kV输电线路的Simulink模型,采用Bergeron模型作为线路参数。接着,利用小波变换提取故障行波的模极大值点,结合凯伦布尔变换消除工频分量,从而精确计算故障位置。文中提供了详细的代码实现步骤,包括信号预处理、小波分解、模极大值检测以及时间差计算等。此外,还讨论了常见调试经验和避坑指南,如采样率设置、接地电阻限制等。最后,通过多个测试案例验证了模型的有效性和准确性。 适合人群:从事电力系统故障诊断的研究人员和技术人员,尤其是对小波变换和凯伦布尔变换感兴趣的工程师。 使用场景及目标:适用于输电线路单相接地故障的精确定位,旨在提高电力系统的可靠性和维护效率。通过掌握本文提供的方法,技术人员能够快速准确地找到故障点,减少停电时间和维修成本。 其他说明:本文不仅提供了理论解释,还包括具体的代码实现和调试建议,有助于读者更好地理解和应用所学知识。

    含分布式电源的IEEE33节点配电网牛拉法潮流计算解析

    内容概要:本文详细介绍了基于牛拉法的含分布式电源(如风能和太阳能)的IEEE33节点配电网潮流计算程序。首先解释了分布式电源的节点类型转换,将光伏处理为PQ节点,风机处理为PV节点。接着阐述了导纳矩阵的构建方法及其对分布式电源的影响,特别是变压器分接头的处理。然后深入探讨了牛拉法的核心迭代过程,包括功率不平衡量的计算、雅可比矩阵的构建以及修正方程的求解。此外,文章还讨论了风光出力波动的处理方法,并分享了一些常见的陷阱及解决方案,如PV节点振荡、孤岛检测和并行计算优化。最后,通过具体实例展示了程序的运行结果,强调了分布式电源接入位置的重要性。 适合人群:从事电力系统仿真、配电网规划与设计的研究人员和技术人员。 使用场景及目标:适用于研究分布式电源对接入配电网的影响,帮助理解和优化含分布式电源的配电网运行特性,提高潮流计算的准确性。 其他说明:文中提供了详细的代码片段和实践经验,有助于读者更好地理解和应用相关理论和技术。同时,文章指出了实际应用中的一些注意事项,如节点类型切换、变压器变比处理等,使读者能够避免常见错误。

Global site tag (gtag.js) - Google Analytics