音频处理七:(极坐标转换)

程序设计七:极坐标转换

一:需求分析

​ 在数学中,极坐标系是一个二维坐标系统。该坐标系统中的点由一个夹和一段相对中心——极点(相当于我们较为熟知的直角坐标系中的原点)的距离来表示。极坐标系的应用领域十分广泛,包括数学物理工程航海以及机器人领域。在两点间的关系用夹角和距离很容易表示时,极坐标系便显得尤为有用;而在平面直角坐标系中,这样的关系就只能使用三角函数来表示。对于很多类型的曲线,极坐标方程是最简单的表达形式,甚至对于某些曲线来说,只有极坐标方程能够表示。

wavtxtifft -i fft.txt -o polor.txt

二:参考知识

1.本地.txt信息
fft_BAC009S0003W0121.txt BAC009S0003W0121.wav语音进行FFT变换后的取值
2.坐标转换后
fft_polar.txt 是fft_BAC009S0003W0121.txt复数转换为极坐标输出(半径 角度)

三:python代码

求取半径

r = y 2 + x 2 r=\sqrt{y^{2}+x^{2}} r=y2+x2

r = np.sqrt(complex_real ** 2 + complex_imag ** 2)

求取角度
tan ⁡ θ = y x \tan \theta=\frac{y}{x} tanθ=xy

angle = np.arctan(complex_imag / complex_real )  # 默认产生的是弧度
angle = angle * (180 / np.pi)  # 弧度转换为角度

完整代码

holiday07.py
import numpy as np
import sys
import getopt
'''
将复数形式的FFT数据转换为极坐标形式
'''
def main(argv):
    try:
         opts, args = getopt.getopt(argv, "-h-i:-o:", ["help", "input=", "output="])
    except getopt.GetoptError:
        print('将读取到的FFT数据,转换为极坐标形式')
        print('python holiday07.py -i fft_BAC009S0003W0121.txt -o fft_polar.txt')
        sys.exit(2)

    # 处理 返回值options是以元组为元素的列表。
    for opt, arg in opts:
        if opt in ("-h", "--help"):
            print("音频数据转换到极坐标")
            print('将读取到的FFT数据,转换为极坐标形式')
            print('python holiday07.py -i fft_BAC009S0003W0121.txt -o fft_polar.txt')
            sys.exit()
        elif opt in ("-i", "--input"):
            input = arg
        elif opt in ("-o", "--output"):
            output = arg

            # fft_BAC009S0003W0121.txt
            complex_array = np.loadtxt(input, dtype=np.complex)

            length = len(complex_array)  # 求N
            file = open(output, 'w')
            for index in range(length):
                complex_real = np.real(complex_array[index])
                complex_imag = np.imag(complex_array[index])
                r = np.sqrt(complex_real ** 2 + complex_imag ** 2)
                angle = np.arctan(complex_imag / complex_real )  # 默认产生的是弧度
                angle = angle * (180 / np.pi)  # 弧度转换为角度
                # angle = np.arctan(complex_imag / complex_real )
                s = '(' + str(r) + ' ' + str(angle) + ')' + '\n'
                file.write(s)
            file.close()



if __name__ == "__main__":
    # sys.argv[1:]为要处理的参数列表,sys.argv[0]为脚本名,所以用sys.argv[1:]过滤掉脚本名。
    main(sys.argv[1:])


#python holiday07.py -i fft_BAC009S0003W0121.txt -o fft_polar.txt

四:实现结果

1.请求帮助
python holiday07.py -h
音频数据转换到极坐标
将读取到的FFT数据,转换为极坐标形式
python holiday07.py -i fft_BAC009S0003W0121.txt -o fft_polar.txt
2.极坐标转换
  • -i 输入FFT数据
  • -o 极坐标数据
python holiday07.py -i fft_BAC009S0003W0121.txt -o fft_polar.txt

五:结果显示及分析

1.结果显示

fft_polar.txt 是fft_BAC009S0003W0121.txt复数转换为极坐标输出(半径 角度)

在这里插入图片描述

2.结果比对

根据公式可以反算出原始数据,以其中第二行数据(384.47795984237615 -46.14453170047742)为例
x = ρ cos ⁡ θ x=\rho \cos \theta x=ρcosθ

y = ρ sin ⁡ θ y=\rho \sin \theta y=ρsinθ

x=384.47795984237615 *cos(-46.14453170047742)=266.38231

y=384.47795984237615*sin(-46.14453170047742)=-277.2431

原始数据为:

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

唐维康

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值