小编jor*_*geh的帖子

在Python中实现复数比较?

我知道一般情况下无法定义具有复数的比较运算符.这就是为什么python TypeError在尝试使用开箱即用的复杂比较时抛出异常的原因.我明白为什么会这样(请不要试图解释为什么两个复数不能比较).

也就是说,在这种特殊情况下,我想根据它们的大小来实现复数比较.换句话说,对于z1z2复杂值,则z1 > z2当且仅假设abs(z1) > abs(z2),其中abs()实现了复数幅值,如在numpy.abs().

我想出了一个解决方案(至少我认为我有)如下:

import numpy as np

class CustomComplex(complex):
    def __lt__(self, other):
        return np.abs(self) < np.abs(other)
    def __le__(self, other):
        return np.abs(self) <= np.abs(other)
    def __eq__(self, other):
        return np.abs(self) == np.abs(other)
    def __ne__(self, other):
        return np.abs(self) != np.abs(other)
    def __gt__(self, other):
        return np.abs(self) > np.abs(other)
    def __ge__(self, other):
        return np.abs(self) >= np.abs(other)

complex = CustomComplex
Run Code Online (Sandbox Code Playgroud)

这似乎有效,但我有几个问题:

  1. 这是要走的路还是有更好的选择?
  2. 我希望我的包透明地使用内置complex数据类型以及numpy.complex.如何在没有代码重复的情况下优雅地完成这项工作?

python numpy complex-numbers

8
推荐指数
1
解决办法
2716
查看次数

WebAudio API采样率

是否可以为实时音频上下文指定采样率(对象的sampleRate属性AudioContext)?

对于我所读的内容,可以指定sampleRate一个OfflineAudioContext对象(构造函数需要3个参数,最后一个是采样率),但实时AudioContext不带任何参数.

我想是不可能的,因为可能是由浏览器本身定义的,但也许有办法?

web-audio-api

7
推荐指数
1
解决办法
3492
查看次数

SoX FIR单缸套

我正在尝试通过与SoX中的脉冲响应进行卷积来应用混响。下面的shell脚本正是我想要的:

#!/usr/bin/env bash
# 
# Convolve audio file with and impulse response (IR)
# 
# Usage:
#
#     applyReverb.sh <ir.wav> <audio.wav> <output.wav>

# IR duration (needed for zero-padding to prevent SoX from cutting 
# the resulting audio after applying the FIR filter
IR_DUR=`soxi -D $1`

# read IR from wav, resample it if necessary, make sure is mono
# and save it as plain text (.dat format in SoX). Also reduces gain 
# to prevent clipping later
sox --norm=-10 -c …
Run Code Online (Sandbox Code Playgroud)

audio shell sox

5
推荐指数
1
解决办法
977
查看次数

如何配置 YAPF 对长参数列表使用悬挂缩进

我使用yapf自动格式化我的 python 代码。总的来说,我对此非常满意,但是有一个样式约定我不知道如何配置。当一对括号内有一长串参数,超出了最大column_limit(例如80)时,我希望它将它们分成单独的行,但如果可能的话保留左括号的缩进。例如:

def func(argument1, argument2, argument3, argument4, argument5, argument6, argument7):
    pass
Run Code Online (Sandbox Code Playgroud)

应该成为

def func(argument1, 
         argument2, 
         argument3,
         argument4,
         argument5,
         argument6,
         argument7):
    pass
Run Code Online (Sandbox Code Playgroud)

但我只能让它做:

def func(
    argument1, 
    argument2, 
    argument3,
    argument4,
    argument5,
    argument6,
    argument7):
    pass
Run Code Online (Sandbox Code Playgroud)

有人知道我想要的是否可能吗?如何?

python yapf

5
推荐指数
1
解决办法
6437
查看次数

标签 统计

python ×2

audio ×1

complex-numbers ×1

numpy ×1

shell ×1

sox ×1

web-audio-api ×1

yapf ×1