判断wav音频是否有离散区间,并标记。麻烦大神帮忙看下代码,总是标记不上

本文介绍了使用Python的Tkinter库创建一个简单的GUI应用程序,用于分析音频文件并检测离散区间。程序读取WAV文件,分割音频为30秒段,计算并显示每个段的幅度变化,用户可以设置离散区间阈值,以识别非正弦波部分。
摘要由CSDN通过智能技术生成
import tkinter as tk
from tkinter import filedialog
import numpy as np
import matplotlib.pyplot as plt
from scipy.io import wavfile

# 设置初始阈值
threshold_value = 0.5

def is_discrete_interval(segment_data, threshold):
    return np.max(segment_data) - np.min(segment_data) >= threshold

def analyze_audio():
    audio_file = filedialog.askopenfilename(title='选择音频文件', filetypes=[('WAV Files', '*.wav')])

    if audio_file:
        sample_rate, data = wavfile.read(audio_file)
        total_duration = len(data) / sample_rate
        segment_duration = 30  # 分段时长为30秒
        num_segments = int(np.ceil(total_duration / segment_duration))

        for i in range(num_segments):
            start_time = i * segment_duration
            end_time = min((i + 1) * segment_duration, total_duration)  # 最后一段按实际长度来计算

            start_index = int(start_time * sample_rate)
            end_index = int(end_time * sample_rate)
            segment_data = data[start_index:end_index].astype(np.float32)

            print(f"第 {i+1} 段的阈值为: {threshold_value}")

            plt.figure(figsize=(8, 4))
            plt.plot(np.arange(start_time, end_time, 1/sample_rate), segment_data, color='blue')
            plt.xlabel('Time (s)')
            plt.ylabel('Amplitude')
            plt.title(f'Segment {i+1}')

            # 计算离散区间的阈值
            threshold = threshold_value * (np.max(segment_data) - np.min(segment_data))

            non_sine_indices = np.where(~is_discrete_interval(segment_data, threshold))[0]

            if len(non_sine_indices) > 0:
                print(f"第 {i+1} 段的离散区间索引值范围:{non_sine_indices}")
                print(f"第 {i+1} 段的离散区间时间范围:{(start_time, end_time)}")

            plt.tight_layout()
            plt.show()

def set_threshold():
    global threshold_value
    threshold_value = float(threshold_entry.get())
    analyze_audio()  # 设置阈值后立即重新分析音频

root = tk.Tk()
root.title("音频分析")
root.geometry("400x300")

threshold_label = tk.Label(root, text="离散区间幅度阈值:")
threshold_label.pack()

threshold_entry = tk.Entry(root)
threshold_entry.pack()

threshold_button = tk.Button(root, text="设置阈值", command=set_threshold)
threshold_button.pack()

file_button = tk.Button(root, text="选择音频文件", command=analyze_audio)
file_button.pack()

root.mainloop()
  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值