C#实现wav波形图

看了一下网上的资料,实现不难,接下来要研究fft

归一按照网上资料,补码/32768

程序分作了两部分,wav格式的定义网上资料很多

读取wav,保存音频数据到txt

using System.IO;
using System;
using System.Text;

namespace 音频处理
{
    class Program
    {
        const int byteSample = 2;
        const int dataPosition = 40;
        //0x16 2byte 0002  双声道
        //0x22 2byte 0010  16位
        //0x18 4byte 0000AC44   44100采样率

        static void Main(string[] args)
        {
            byte[] length = new byte[4];
            FileStream fs = new FileStream("test.wav", FileMode.Open, FileAccess.Read);
            fs.Position = dataPosition;
            fs.Read(length, 0, 4);
            byte[] content = new byte[getHexToInt(length)];
            string[] sample = new string[content.Length / byteSample];
            fs.Read(content, 0, content.Length);
            getHex(content);
            sample = getSample(content);
            StreamWriter sw = new StreamWriter("data.txt", true, Encoding.Default);
            foreach (string i in sample)
            {
                sw.Flush();
                sw.WriteLine(i);
            }
            sw.Close();
        }

        static int getHexToInt(byte[] x)
        {
            string retValue = "";
            for (int i = x.Length - 1; i >= 0; i--)
            {
                retValue += x[i].ToString("X");
            }
            return Convert.ToInt32(retValue, 16);
        }

        static void getHex(byte[] x)
        {
            byte tmp;
            for (int i = 0; i < x.Length; i++)
            {
                tmp = Convert.ToByte(x[i].ToString("X"), 16);
                x[i] = tmp;
            }
        }

        static string[] getSample(byte[] x)
        {
            string[] retValue = new string[x.Length / byteSample];

            for (int i = 0; i < retValue.Length; i++)
            {
                for (int j = (i + 1) * byteSample - 1; j >= i * byteSample; j--)
                {
                    retValue[i] += x[j].ToString("X");
                }
                retValue[i] = ((double)Convert.ToInt16(retValue[i], 16) / 32768).ToString("F4");
            }
            return retValue;
        }
    }
}


读取txt,显示波形

using System;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections.Generic;
using System.Threading;

namespace LoadDataView
{
    public partial class Form1 : Form
    {
        const bool leftStatus = false;

        public Form1()
        {
            InitializeComponent();
            new Thread(new ThreadStart(() =>
            {
                this.Invoke(new MethodInvoker(() => { label1.Text = leftStatus ? "左声道" : "右声道"; }));
                List<double> list = new List<double>();
                StreamReader sr = new StreamReader("data.txt", Encoding.Default);
                int m = 0;
                while (!sr.EndOfStream)
                {
                    if (leftStatus)
                    {
                        if (m % 2 == 0)
                            list.Add(double.Parse(sr.ReadLine()));
                    }
                    else
                    {
                        if (m % 2 != 0)
                            list.Add(double.Parse(sr.ReadLine()));
                    }
                    m++;
                }
                sr.Close();
                Bitmap bitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);
                Graphics g = Graphics.FromImage(bitmap);
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                g.DrawLine(new Pen(Color.Black, 5), new Point(20, 20), new Point(20, 540));
                g.DrawLine(new Pen(Color.Black, 5), new Point(20, 290), new Point(1600, 290));

                int k = list.Count / 1550;

                for (int i = 0; i < list.Count; i++)
                {
                    g.DrawLine(new Pen(Color.Green, 1), new Point(20 + i / k, 290), new Point(20 + i / k, 290 + (int)(list[i] * 250 * 2)));
                }
                this.pictureBox1.Image = bitmap;
            })).Start();
        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            this.Width = 1650;
            this.Height = 600;
        }
    }
}


  • 0
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
C#可以通过数学计算的方式实现各种波形信号的算法。下面是一些常见的波形信号及其算法实现: 1. 正弦信号: 正弦信号的数学表达式为:y = A*sin(2πf*t+φ) 其中,A为振幅f为频率,t为时间,φ为初相位。 C#代码实现: ``` double A = .0; // 振幅 double f = 50.0; // 频率 double phi = 0.0;// 初相位 double t = 0.0; // 时间 double y = 0.0; // 正弦信号 for (int i = 0; i < n; i++) { t = i * dt; // dt为采样时间间隔 y = A * Math.Sin(2 * Math.PI * f * t + phi); // 将y存储到数组中或输出到文件中 } ``` 2. 方波信号: 方波信号的数学表达式为: y = A*sign(sin(2πf*t+φ)) 其中,sign为符号函数,A、f、φ和t的意义与正弦信号相同。 C#代码实现: ``` double A = 1.0; // 振幅 double f = 50.0; // 频率 double phi = 0.0;// 初相位 double t = 0.0; // 时间 double y = 0.0; // 方波信号 for (int i = 0; i < n; i++) { t = i * dt; // dt为采样时间间隔 y = A * Math.Sign(Math.Sin(2 * Math.PI * f * t + phi)); // 将y存储到数组中或输出到文件中 } ``` 3. 三角波信号: 三角波信号的数学表达式为: y = A*asin(sin(2πf*t+φ)) 其中,asin为反正弦函数,A、f、φ和t的意义与正弦信号相同。 C#代码实现: ``` double A = 1.0; // 振幅 double f = 50.0; // 频率 double phi = 0.0;// 初相位 double t = 0.0; // 时间 double y = 0.0; // 三角波信号 for (int i = 0; i < n; i++) { t = i * dt; // dt为采样时间间隔 y = A * Math.Asin(Math.Sin(2 * Math.PI * f * t + phi)); // 将y存储到数组中或输出到文件中 } ``` 以上是一些常见的波形信号及其算法实现,实际应用中还可以根据具体需求进行调整和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值