python 处理阻尼正弦

阻尼正弦
阻尼正弦波(英語:damped sine wave)是振幅會隨時間增長而趨向零的正弦波函數。當諧振子消耗的能量比供應的能量多,其波形即為阻尼正弦波,此函數常用在科學及工程中。
阻尼正弦可以定义为:x(t)=sin(2πft)e^(−αt)
以下代码生成正弦信号和衰减形状。 通过将两个信号逐个样本相乘来生成阻尼正弦信号。

fs=8000
t=np.arange(0,1,1/fs) # crete time vector of 1 second length
f = 2.5               # 2.5 Hz
alpha = 1             # daming factor (play around with it and see what happens)sin  = np.sin(2*np.pi*f*t)
damp = np.exp(-alpha*t)
x = sin * dampplt.subplot(2,1,1)
plt.plot(t,sin)
plt.ylabel('sin($t$)')plt.subplot(2,1,2)
plt.plot(t,x, label='sin$(2 \pi \cdot ' + str(f) + ' \mathrm{Hz} \cdot t) \cdot \mathrm{e}^{- a t}$')
plt.plot(t,damp, '--', label='$\mathrm{e}^{- a t}$')
plt.legend()
plt.xlabel('$t$ in seconds')

exp:高等数学里以自然常数e为底的指数函数
numpy.exp():返回e的幂次方,e是一个常数为2.71828

  • why sin = np.sin(2np.pif*t) inside the bracket it still need multiply np?

首先没有阻尼。

concert_pitch_1sec = get_sine_wave(440, 1, 8000)
half_concert_pitch_1sec = get_sine_wave(220, 1, 8000)alternation_1sec = np.concatenate((half_concert_pitch_1sec, concert_pitch_1sec,half_concert_pitch_1sec, concert_pitch_1sec))#plot the tune
plt.plot(alternation_1sec) # you can't see much here, it will get clear together with the next plotipd.Audio(alternation_1sec, rate=8000)
  • why inside the np.concatenate() function, half_concert_pitch_1sec, concert_pitch_1sec are repeated for twice.
  • why plot them display like a rectangle

下面我们可以观察到阻尼对音乐音调的影响——阻尼合成的曲调听起来更“自然”。 (此技术用于 MIDI 乐器合成)。

alternation_damped = np.concatenate((half_concert_pitch_1sec* damp, concert_pitch_1sec* damp,half_concert_pitch_1sec* damp, concert_pitch_1sec* damp))#plot the tune
plt.plot(alternation_damped)ipd.Audio(alternation_damped, rate=8000)

通过使用 get_sine_wave() 函数生成不同长度和频率的正弦波,您现在可以播放简单的歌曲。正如实验表中已经描述的那样,每个音符都有一个基本的 [频率]

#create musical notes
g = get_sine_wave(196.00)
a = get_sine_wave(220)
b = get_sine_wave(246.94)
d = get_sine_wave(293.66)
c = get_sine_wave(261.63)
e = get_sine_wave(329.63)
#append together to form tune + chord
tune = [a,d,a,g,a,b,d,(g+b+d)]
tune = np.concatenate(tune)
#plot the tune
plt.plot(tune)
#audio player for the tune
ipd.Audio(tune,rate=8000) #我们可以再次观察到阻尼对音乐音调的影响。
#create dampened musical notes
g = get_sine_wave(196.00)*damp
a = get_sine_wave(220)*damp
b = get_sine_wave(246.94)*damp
d = get_sine_wave(293.66)*damp
c = get_sine_wave(261.63)*damp
e = get_sine_wave(329.63)*damp#create tune
tune = [b,d,a,g,a,b,d,(g+b+d)]
tune = np.concatenate(tune)
#plot dampened tune waveform
plt.plot(tune)
#audio player for dampened tune
ipd.Audio(tune,rate=8000)
  • why g a b c d e have different value in get_sine_wave() but they print same plots

音符的长度由减半给出:一个二分音符的长度是一个全音符的一半,一个四分音符是半个二分音符(或一个全音符的四分之一)等。音符时长通常也可以更复杂,例如 为 3/4 或 3/2。
您应该为整个音符选择合适的 fs_Hz 和持续时间。 特别要确保将 fs_Hz 与十六分音符的持续时间相乘得到一个整数结果,不需要四舍五入。
我们需要一个简单的附加功能来生成更高级的歌曲是生成静音的功能。 以下函数 get_silence() 生成所需长度(以秒为单位)的 0 值数组。

right_hand_notes = [("E5", 1 / 16),("D#5", 1 / 16),("E5", 1 / 16),("D#5", 1 / 16),("E5", 1 / 16),("B4", 1 / 16),("D5", 1 / 16),("C5", 1 / 16),("A4", 1 / 8),("Pause", 1 / 16),("C4", 1 / 16),("E4", 1 / 16),("A4", 1 / 16),("B4", 1 / 8),("Pause", 1 / 16),("E4", 1 / 16),("G#4", 1 / 16),("B4", 1 / 16),("C4", 1 / 8),("Pause", 1 / 16),("E4", 1 / 16),("E5", 1 / 16),("D#5", 1 / 16),("E5", 1 / 16),("D#5", 1 / 16),("E5", 1 / 16),("B4", 1 / 16),("D5", 1 / 16),("C5", 1 / 16),("A4", 1 / 8),("Pause", 1 / 16),("C4", 1 / 16),("E4", 1 / 16),("A4", 1 / 16),("B4", 1 / 8),("Pause", 1 / 16),("E4", 1 / 16),("C5", 1 / 16),("B4", 1 / 16),("A4", 1 / 4),
]left_hand_notes = [("Pause", 1 / 8),("Pause", 3 / 8),("A2", 1 / 16),("E3", 1 / 16),("A3", 1 / 16),("Pause", 3 / 16),("E2", 1 / 16),("E3", 1 / 16),("G#3", 1 / 16),("Pause", 3 / 16),("A2", 1 / 16),("E3", 1 / 16),("B3", 1 / 16),("Pause", 3 / 16),("Pause", 3 / 8),("A2", 1 / 16),("E3", 1 / 16),("A3", 1 / 16),("Pause", 3 / 16),("E2", 1 / 16),("E3", 1 / 16),("G#3", 1 / 16),("Pause", 3 / 16),("A2", 1 / 16),("E3", 1 / 16),("B3", 1 / 16),("Pause", 1 / 16),
]def get_silence(length_s, sample_rate_hz):"""Return silence for the given length at the given sample rate."""return np.zeros(int(length_s * sample_rate_hz))

np.zeros() 可以创建一维数组,例如np.zeros(5) -> [0. 0. 0. 0. 0.]
np.zeros() 也可以创建多维数组,例如np.zeros((5,2))
多维数组结果是:
[[0. 0.]
[0. 0.]
[0. 0.]
[0. 0.]
[0. 0.]]

现在我们可以定义一个函数来创建所需的音符或暂停,由上面对 left_hand_notes 和 right_hand_notes 的描述定义。

请注意,所需信号频率由下式计算
f=440⋅2^((note−position/12)+(octave−4))

def create_tone(note, duration):tempo = 5if note == "Pause":return get_silence(length_s=duration*tempo, sample_rate_hz=8000)note_position = {"C": -9,"C#": -8,"D": -7,"D#": -6,"E": -5,"F": -4,"F#": -3,"G": -2,"G#": -1,"A": 0,"A#": 1,"B": 2,}octave = int(note[-1])key = note[:-1]frequency_hz = 440 * 2 ** ((note_position[key] / 12) + (octave - 4))return get_sine_wave(frequency_Hz=frequency_hz, length_s=duration*tempo, fs_Hz=8000)

创建一个测试音以查看上述功能是否按预期工作。

test_tone=create_tone("A4", 1/8)plt.plot(test_tone[1:500])
plt.title('test tone A4 (first 500 samples)')
plt.xlabel('sample index $k$')
ipd.Audio(test_tone, rate=8000)
  • why change the value of test_tone, the graph is still same with original

现在让我们来听一首完整的歌曲。我们可以将歌曲的双手相加得到完整的歌曲。如果您愿意,现在可以更改代码以包含阻尼。

right_hand = np.concatenate([create_tone(note, duration) for note, duration in right_hand_notes]
)left_hand = np.concatenate([create_tone(note, duration) for note, duration in left_hand_notes]
)song = left_hand + right_hand
ipd.Audio(song, rate=8000)

以下(已知)命令可用于将 WAVE 文件下载到硬盘和内存中。
请注意,如果要将变量 file_name 传递给 curl 命令,则必须将其放在大括号中。 这允许您重新使用下载命令

file_name = 'music_44k.wav'
!curl https://staffwww.dcs.shef.ac.uk/people/S.Goetze/sound/{file_name} -o {file_name}
import soundfile as sf # for loading wave files from hard disk# download
file_name = 'music_44k.wav'
print('Downloading file: ' + file_name)
!curl https://staffwww.dcs.shef.ac.uk/people/S.Goetze/sound/{file_name} -o {file_name}# load WAVE files
mus_44k, fs44 = sf.read(file_name)print('The sampling frequency is ' + str(fs44) + ' Hz')# This is how our speech signal looks like
plt.plot(mus_44k);
plt.xlabel('sample index $k$');

我们使用 librosa 库来改变采样频率 fsf_sfs​。 以下代码首先将声音信号下采样到 fs=8f_s=8fs​=8 kHz。 通过这样做,所有高于 fmax=4f_{\mathrm{max}}=4fmax​=4 kHz 的信息,即采样频率的一半,都会丢失。 即使我们再次上采样到 fs=44.1f_s=44.1fs​=44.1 kHz 的原始采样频率,丢失的信息也不会恢复。

import librosa         # we will use the library librosa here for resampling#donwsample from 44.1 kHz to 8 kHz
mus_8k = librosa.resample(mus_44k, orig_sr=fs44, target_sr=8000); # resample to 8 kHz
#upsample from 8 kHz to 44.1 kHz
mus_44k_2 = librosa.resample(mus_8k, orig_sr=8000, target_sr=fs44); # resample to 44.1 kHz# visualise
fig=plt.figure(figsize=(12,6)) # create a figure of size 12 x 6 inchesplt.subplot(1,3,1)
plt.specgram(mus_44k, Fs=fs44);
plt.title('original')
plt.xlabel('time $t$')
plt.ylabel('frequency $f$')plt.colorbar(label='dB');
plt.clim(-150,0)plt.subplot(1,3,2)
plt.specgram(mus_8k, Fs=8000);
plt.title('downsampled to $8000$ Hz')
plt.xlabel('time $t$')
plt.ylabel('frequency $f$')plt.colorbar(label='dB');
plt.clim(-150,0)plt.subplot(1,3,3)
plt.specgram(mus_44k_2, Fs=fs44);
plt.title('after down-/upsampling')
plt.xlabel('time $t$')
plt.ylabel('frequency $f$')plt.colorbar(label='dB');
plt.clim(-150,0)plt.tight_layout() # this command ensures that labelsdon't overlap

上面的三个时频表示[频谱图]可视化了下采样和上采样的效果。左侧面板显示了在 fs=44100Hzf_s=44100 \,\mathrm{Hz}fs​=44100Hz 处采样的原始波形文件的扇区图。您可以看到信号中存在的能量频率高达 12fs=22050Hz\frac{1}{2} f_s = 22050 \, \mathrm{Hz}21​fs​=22050Hz(由 Nyquist-Shannon 采样定理:fmax=12fsf_{\mathrm{max}} = \frac{1}{2} f_sfmax​=21​fs​)。中间面板显示了下采样到 fs=8000Hzf_s=8000 \,\mathrm{Hz}fs​=8000Hz 的信号,因此频率高达 fmax=4000Hzf_{\mathrm{max}}=4000 \,\mathrm{Hz}fmax​=4000Hz 的信号能量存在于信号(参见不同的 yyy 轴)。再次将此信号上采样到 fs=44100Hzf_s=44100 \,\mathrm{Hz}fs​=44100Hz 的原始采样频率(参见右图)后,您会看到频率高于 f=4000Hz的信号能量f=4000 \,\mathrm{Hz} 的信号能量f=4000Hz的信号能量 不会再次恢复,但会在下采样/上采样过程中丢失。
这可以通过播放音频文件来感知。

python 处理阻尼正弦相关推荐

  1. python正弦函数拟合_在Python中拟合正弦数据

    我想将下面附带的数据与-a*sin(b*x + c)(或可能也可以使用-a*sin(2*x))与a b c作为要确定的值的函数拟合.我使用了scipy.optimize.curve_fit,但效果不好 ...

  2. python程序画漂亮图_用python画图代码:正弦图像、多轴图等案例

    python画图 正弦图像: #coding:utf-8 import numpy as np import matplotlib.pyplot as plt x=np.linspace(0,10,1 ...

  3. python——循环结构正弦幂级数展开及图像

    QQ 1274510382 Wechat JNZ_aming 商业互捧 QQ群538250800 技术搞事 QQ群599020441 技术合作 QQ群152889761 加入我们 QQ群6493473 ...

  4. L-BFGS算法/Broyden族/BFGS算法/阻尼牛顿法的Python实现代码

    下面定义了三个Python语言编写的函数:函数表达式fun,梯度向量gfun,和海森矩阵hess.这三个表达式在后面各个算法的实现中会用到. # 函数表达式fun fun = lambda x:100 ...

  5. 浅析信号与系统1(指数信号与正弦信号)

    目录 1. 连续时间信号与离散时间信号 2. 自变量的变换 3. 周期信号 4. 偶信号与奇信号 5. 指数信号与正弦信号 实指数信号 周期复指数和正弦信号 例 1. 连续时间信号与离散时间信号 连续 ...

  6. 用Python做信号处理

    用Python做信号处理 声明:本文中设计的知识和代码大部分来自:芥末的无奈的博客_CSDN博客-音频处理,c++,keras领域博主 以及 凌逆战 - 博客园 (cnblogs.com) 两位大神所 ...

  7. sinusoidal sweep正弦扫频信号

    相对于单音信号,正弦扫频信号即为在一定的频段范围内频率连续变化的信号.正弦扫频信号可作为系统激励和测取系统传递函数的较好方法.它主要实现对元器件.电路及整机的频率特性的测试,广泛应用在科研及生产等多个 ...

  8. 二阶自回归过程matlab,时间序列分析:二阶自回归过程

    时间序列分析:二阶自回归过程 Author: nex3z 2019-07-13 1. 定义 对于二阶自回归过程 $AR(2)$ \begin{equation} X_t = \phi_1 X_{t-1 ...

  9. 机器学习--梯度-牛顿-拟牛顿优化算法和实现

    版权声明:作者:Jinliang's Hill(金良山庄),欲联系请评论博客或私信,CSDN博客: http://blog.csdn.net/u012176591 目录(?)[+] 要求解的问题 线搜 ...

  10. 自控考研复习 自我梳理(三) 知识来自网络,纯为总结侵权即删(二阶系统)

    第三章: 本章主要介绍了线性系统的时域分析,从系统的稳态入手讲述了几个系统的评价标准.从一阶入手,升至二阶一直于多阶系统.其中二阶系统最为重要.本章关键知识点有稳态系统误差求解,判断系统是否稳定的劳斯 ...

最新文章

  1. 2022-2028年中国氨基酸表面活性剂行业研究及发展前瞻报告
  2. max函数的平滑(log-sum-exp trick)
  3. CRM lifecycle status
  4. python爬虫天气数据_python爬虫:天气数据的分析
  5. 波的折射现象,你都了解吗?
  6. 《统计学习方法》(李航)读书笔记(转)
  7. 再回首Java第十七天
  8. Python的系统管理_08_python_异常处理
  9. 使用getString成员函数获取表字段数据出错
  10. python怎么重新运行,如何让python程序重新运行其
  11. EXE文件反编译工具下载
  12. 离散数学_命题逻辑_部分习题
  13. cad2012打开后闪退_【电脑安装好cad一打开闪退】cad安装完打开闪退_cad2012打开闪退...
  14. 揭秘微信身份证背后的高科技——人脸识别技术
  15. Nginx系列:windows10系统下安装nginx的安装并配置!
  16. 团队作业第二次—团队展示
  17. 基于ESP32的蓝牙翻页器设计(论文附调试成功代码!!)
  18. 今日份安利:视频变声的软件有哪些?
  19. 测试结果OK POK NG NT的意义
  20. code回归采访哭 ladies_LADIES’CODE再次提到高恩妃和权梨世的车祸事故 在节目中流泪...

热门文章

  1. 显示upnp服务器 sonos,蒲公英的上层设备如何开启UPnP及其优点
  2. canvas制作呼吸灯
  3. 腾讯天龙八部手游服务器账号上线,天龙八部手游服务器的注册已达到上限 服务器注册上限怎么解决...
  4. 大型网站之网站静态化(综合篇)
  5. Latex 学术撰写工具推荐(在线、Windows、Mac、Linux)
  6. JZOJ 5750 青青草原播种计划
  7. sublime使用技巧
  8. 网络安全与渗透:sql注入,一文详解(九)此生无悔入华夏,男儿何不带吴钩
  9. 华为鸿蒙系统什么架构,你知道华为鸿蒙到底是什么吗?
  10. input标签 各属性解释