音频频率时长

import librosa
import numpy as np

# 加载音频文件
file_name = 'your_audio_file.wav'
y, sr = librosa.load(file_name, sr=None)  # 加载文件,sr=None保持原始采样率

# 执行短时傅里叶变换(STFT)
n_fft = 2048  # FFT窗口大小
hop_length = 512  # 每次滑动的样本数
stft_result = librosa.stft(y, n_fft=n_fft, hop_length=hop_length)
stft_magnitude, stft_phase = librosa.magphase(stft_result)

# 获取频率轴
frequencies = librosa.fft_frequencies(sr=sr, n_fft=n_fft)

# 指定要检测的频率
target_frequency = 1000  # 以赫兹为单位

# 找到最接近目标频率的索引
target_index = np.argmin(np.abs(frequencies - target_frequency))

# 提取目标频率的幅度
target_frequency_magnitude = stft_magnitude[target_index, :]

# 设置一个阈值来确定频率何时被认为是“存在”的
threshold = 0.1 * np.max(target_frequency_magnitude)

# 检测目标频率何时超过阈值
presence = target_frequency_magnitude > threshold

# 计算目标频率的持续时间
durations = librosa.frames_to_time(np.where(presence)[0], sr=sr, hop_length=hop_length)

# 如果需要,计算总持续时间
total_duration = np.sum(np.diff(durations))

print(f"Total duration of frequency {target_frequency} Hz is {total_duration:.2f} seconds.")
import librosa
import numpy as np

# Load the audio file
file_name = 'your_audio_file.wav'
y, sr = librosa.load(file_name, sr=None)  # Load the file, sr=None to keep the original sample rate

# Perform Short-Time Fourier Transform (STFT)
n_fft = 2048  # FFT window size
hop_length = 512  # Number of samples to slide each time
stft_result = librosa.stft(y, n_fft=n_fft, hop_length=hop_length)
stft_magnitude, stft_phase = librosa.magphase(stft_result)

# Get the frequency axis
frequencies = librosa.fft_frequencies(sr=sr, n_fft=n_fft)

# Specify the frequency to detect
target_frequency = 1000  # in Hertz

# Find the index closest to the target frequency
target_index = np.argmin(np.abs(frequencies - target_frequency))

# Extract the magnitude for the target frequency
target_frequency_magnitude = stft_magnitude[target_index, :]

# Set a threshold to determine when the frequency is considered "present"
threshold = 0.1 * np.max(target_frequency_magnitude)

# Detect when the target frequency exceeds the threshold
presence = target_frequency_magnitude > threshold

# Find the start and end frames of each segment where the frequency is present
presence_diff = np.diff(presence.astype(int), prepend=0, append=0)
start_frames = np.where(presence_diff == 1)[0]
end_frames = np.where(presence_diff == -1)[0]

# Convert frames to time
start_times = librosa.frames_to_time(start_frames, sr=sr, hop_length=hop_length)
end_times = librosa.frames_to_time(end_frames, sr=sr, hop_length=hop_length)

# Output the specific time segments
time_segments = list(zip(start_times, end_times))

# Print the time segments
for i, (start, end) in enumerate(time_segments):
    print(f"Segment {i+1}: Start at {start:.2f} seconds, end at {end:.2f} seconds.")
import librosa
import numpy as np

# 加载音频文件
audio_path = 'your_audio_file.wav'
y, sr = librosa.load(audio_path, sr=None)

# 计算短时傅里叶变换(STFT)
D = librosa.stft(y)

# 获取振幅
S, phase = librosa.magphase(D)

# 转换为频率和时间
frequencies = librosa.fft_frequencies(sr=sr)
times = librosa.frames_to_time(np.arange(S.shape[1]), sr=sr)

# 指定频率段
freq_low = 1000  # 频率下限
freq_high = 2000  # 频率上限

# 找到对应频率段的索引
freq_indices = np.where((frequencies >= freq_low) & (frequencies <= freq_high))[0]

# 求和特定频率段的能量
target_band_energy = np.sum(S[freq_indices, :], axis=0)

# 设置一个阈值来确定特定频率段是否"活跃"
energy_threshold = np.max(target_band_energy) * 0.5

# 检测起止时间
active_frames = np.where(target_band_energy > energy_threshold)[0]
diff_frames = np.diff(active_frames)

# 找到连续的帧
segment_boundaries = np.where(diff_frames > 1)[0]
start_frames = np.insert(active_frames[segment_boundaries + 1], 0, active_frames[0])
end_frames = np.append(active_frames[segment_boundaries], active_frames[-1])

# 转换帧到时间
start_times = times[start_frames]
end_times = times[end_frames]
durations = end_times - start_times

# 打印结果
for i, (start, end, duration) in enumerate(zip(start_times, end_times, durations)):
    print(f"Segment {i}: Start time: {start:.2f}s, End time: {end:.2f}s, Duration: {duration:.2f}s")

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值