# BUG 1024
import os
import sys
import wave
import time
import ui_cn_1
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
from scipy import signal
from PyQt5.QtCore import *
from scipy.io import wavfile
from PyQt5.QtWidgets import *
from PyQt5.QtMultimedia import *
from QCandyUi import CandyWindow
from PyQt5.QtGui import QTextCursor
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from PyQt5.QtWidgets import QApplication, QMainWindow, QFileDialog, QComboBox, QCheckBox
matplotlib.rcParams['font.family'] = 'STSong'
matplotlib.use('Qt5Agg')
# 处理函数
class ProcessFunction(object):
# 归一化幅度
def Audio_TimeDomain(self, feature): # 时域
f = wave.open(feature.path, "rb") # 打开音频文件
params = f.getparams()
nchannels, sampwidth, framerate, nframes = params[:4]
# nchannels通道数
# sampwidth量化位数
# framerate采样频率
# nframes采样点数
str_data = f.readframes(nframes) # 返回Bytes对象表示的采样点数设置的音频
f.close() # 关闭音频文件
# 将字符串转换为数组,得到一维的short类型的数组
wave_data = np.fromstring(str_data, dtype=np.short) # dtype = s
# 赋值的归一化
wave_data = wave_data * 1.0 / (max(abs(wave_data)))
# 整合左声道和右声道的数据
# numpy.reshape(a, newshape, order='C') [采样点数, 通道数] 返回值为 *采样点数* 维的数组,每组有 *通道数* 个
wave_data = np.reshape(wave_data, [nframes, 1])
# 最后通过采样点数和取样频率计算出每个取样的时间
time = np.arange(0, nframes) * (1.0 / framerate)
# 输出音频信息
feature.textBrowser_3.append("音频信息: " + str(nchannels))
feature.textBrowser_3.append("通道数: " + str(nchannels))
feature.textBrowser_3.append("采样频率: " + str(framerate) + " Hz")
feature.textBrowser_3.append("采样点数: " + str(nframes))
feature.textBrowser_3.append("采样时间: " + str(nframes / framerate) + " seconds")
# 进度条设置10
feature.progressBar.setValue(10)
feature.progressBar_2.setValue(10)
# 设置画布
ax = feature.fig1.add_subplot(111)
# 调整图像大小
ax.cla()
# 画出归一化幅度图像
ax.plot(time, wave_data[:, 0], color='#dc143c')
ax.set_title('归一化幅度', fontsize=10)
ax.set_xlabel('时间[sec]', fontsize=10)
feature.fig1.subplots_adjust(left=0.1, bottom=0.215, right=None, top=None, wspace=None, hspace=None)
feature.canvas1.draw()
#取一段时间进行分析
# 设置画布
bx = feature.fig8.add_subplot(111)
# 调整图像大小
bx.cla()
# 画出归一化幅度图像
StartTime0 = np.int(feature.StartTime)
DurationTime0 = np.int(feature.DurationTime)
StartTime1 = np.int((framerate / 1000) * StartTime0)
DurationTime1 = np.int((StartTime1 + DurationTime0 * (framerate / 1000)))
# 传入数组的变量必须是整型,而乘除法返回的是浮点,用int()转换
# print(StartTime1)
# print(DurationTime1)
# 实现20ms的幅度显示
bx.plot(time[StartTime1:DurationTime1], wave_data[StartTime1:DurationTime1, 0], color='#5014DC')
bx.set_title('归一化幅度', fontsize=10)
bx.set_xlabel('时间[sec]', fontsize=10)
feature.fig8.subplots_adjust(left=0.1, bottom=0.215, right=None, top=None, wspace=None, hspace=None)
feature.canvas8.draw()
# 进度条设置20
feature.progressBar.setValue(20)
feature.progressBar_2.setValue(20)
# 频域,FFT
def Audio_FrequencyDomain(self, feature):
# STFT图像绘制
sampling_freq, audio = wavfile.read(feature.path) # 返回采样频率和一维数组,对应8-bit integer PCM编码
T = 20 # 短时傅里叶变换的时长 单位 ms
fs = sampling_freq # 采样频率
N = len(audio) # 采样点的个数
audio = audio * 1.0 / (max(abs(audio))) # 归一化
# 计算并绘制STFT的大小
# nperseg :int, optional
# 段长. 默认256 .
# f:ndarray
# Array of sample frequencies.
# t:ndarray
# Array of segment times.
# Zxx:ndarray
# x的STFT. 一般情况下,Zxx 的最后一个轴对应段落时间 .
f, t, Zxx = signal.stft(audio, fs, nperseg=np.int(T * fs / 1000))
# 设置进度条30
feature.progressBar.setValue(30)
feature.progressBar_2.setValue(30)
ax = feature.fig5.add_subplot(111)
feature.fig5.subplots_adjust(left=None, bottom=0.215, right=None, top=None, wspace=None, hspace=None)
ax.cla()
ax.pcolormesh(t, f, np.abs(Zxx), vmin=0, vmax=0.1) # 明显划分出边缘,表现为颜色深浅
ax.set_title('短时傅里叶变换STFT', fontsize=10)
ax.set_xlabel('时间 [sec]', fontsize=10)
ax.set_ylabel('频率 [Hz]', fontsize=10)
feature.canvas5.draw()
feature.progressBar.setValue(40)
feature.progressBar_2.setValue(40)
# 绘制FFT图像
fft_signal = np.fft.fft(audio)
fft_signal = abs(fft_signal) # 取绝对值
# 建立频率轴
# 复信号没有负频率,以fs为采样速率的信号,fft的频谱结果是从[0,fs]的。
fft_signal = np.fft.fftshift(fft_signal) # 移动0频率点,画图
# 与上同理
fft_signal = fft_signal[int(fft_signal.shape[0] / 2):]
# 频率轴的间隔
freqInteral = (sampling_freq / len(fft_signal))
Freq = np.arange(0, sampling_freq / 2, sampling_freq / (2 * len(fft_signal)))
# 设置进度条50
feature.progressBar.setValue(50)
feature.progressBar_2.setValue(50)
# 计算最高频率
highFreq = (np.argmax(fft_signal[int(len(fft_signal) / 2):len(fft_signal)])) * freqInteral
feature.textBrowser_3.append("FFT : 最高频率为: " + str(highFreq))
ax = feature.fig3.add_subplot(111)
# 调整图像大小
ax.cla()
ax.plot(Freq, fft_signal, color='#dc143c')
ax.set_title('FFT 图像', fontsize=10)
ax.set_xlabel('频率 [Hz]', fontsize=10)
ax.set_ylabel('增益', fontsize=10)
feature.fig3.subplots_adjust(left=None, bottom=0.215, right=None, top=None, wspace=None, hspace=None)
feature.canvas3.draw()
cx = feature.fig10.add_subplot(111)
# 调整图像大小
cx.cla()
cx.plot(Freq, fft_signal, color='#5014DC')
cx.set_title('FFT 图像', fontsize=10)
cx.set_xlabel('频率 [Hz]', fontsize=10)
cx.set_ylabel('强度 [dB]', fontsize=10)
feature.fig10.subplots_adjust(left=None, bottom=0.215, right=None, top=None, wspace=None, hspace=None)
feature.canvas10.draw()
# 设置进度条60
feature.progressBar.setValue(60)
feature.progressBar_2.setValue(60)
# 语谱图
def Audio_SpectrogramDomain(self, feature):
# 语谱图绘制
f = wave.open(feature.path, "rb")
params = f.getparams()
nchannels, sampwidth, framerate, nframes = params[:4]
strData = f.readframes(nframes) # 读取音频,字符串格式
waveData = np.fromstring(strData, dtype=np.int16) # 将字符串转化为int
waveData = waveData * 1.0 / (max(abs(waveData))) # wave幅值归一化
waveData = np.reshape(waveData, [nframes, 1]).T # 合并多声道,.T:数组转置,横坐标为时间 ,变成一维数组,下面会用
一个DSP滤波器设计工具,可以设计多种FIR和IIR滤波器
需积分: 0 34 浏览量
2023-10-28
10:00:27
上传
评论
收藏 203KB ZIP 举报
![avatar](https://profile-avatar.csdnimg.cn/738a1e16760e462eb098be8dda95fd19_ahah12345678.jpg!1)
Older司机渣渣威
- 粉丝: 8
- 资源: 202
最新资源
- 高分毕设-基于Python+OpenCV人脸识别考勤打卡系统源代码(95分以上).zip
- opencv项目实战:使用OpenCV实现哈哈镜效果(包含源码+说明文档+教程.zip)
- 微软运行库2024.exe
- Axure RP 9是一款专业的快速原型设计工具,安装即用
- 基于JSP+Servlet+Tomcat9.0+IDEA+Mysql实现的Javaweb图书管理系统
- Toon Boom Storyboard Pro 是一款专业的故事板制作软件,专为动画制作和影视前期制作而设计
- Serif Affinity Designer 是一款专业的矢量图形设计软件,旨在提供强大的创意工具和无与伦比的性能
- dbeaver exe安装脚本
- 基于Python+OpenCV人脸识别考勤管理系统+源代码(高分毕业设计).zip
- Photoshop 900个矢量和Photoshop画笔
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)