剪切整个歌曲的特定频率,使用一个样本来选择要删除的频率

2024-04-25 17:32:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试着用Numpy和Librosa从一首歌的一个窗口中提取一个样本,从这首歌中剪切一些特定的频率。
解释:我有一首4:30分钟的歌,我在这首歌中选择了一个2.667秒的样本,我想删除这个样本中出现的频率。你知道吗

导入我的歌曲后,我遇到了一个删除歌曲中特殊频率的问题:我的样本长度小于整个歌曲的长度,因此我无法从歌曲中减去样本的频率(我认为这是删除频率的一个好方法)。你知道吗

你知道我该怎么办吗? 以下是我目前的代码:

import numpy as np
import librosa.display           

# song importation
y, sr = librosa.load("my_song.wav")                            # y: audio time series, sr: sampling rate

# creation of a sample
song_duration = librosa.get_duration(y=y, sr=sr)               # duration of song in seconds
sample_start = 0.384                                           # start of the sample (in seconds) here: 0.384
sample_end = 3.051                                             # end   of the sample (in seconds) here: 3.051
sample_duration = sample_end - sample_start                    # sample duration in seconds
position_start = int((sample_start*len(y))/song_duration)                     # the starting position of the sample
position_end = position_start + int((sample_duration*len(y))/song_duration)   # the end of the sample
sample = y[position_start:position_end]                        # the sample (fraction of y)
scaled = np.int16(sample/np.max(np.abs(sample)) * sr)          # need to scale to have integer and not float

fft_y = np.fft.fft(y)                                          # 1D discrete Fourier Transform of entire song
fft_sample = np.fft.fft(sample)                                # 1D discrete Fourier Transform of the sample
n_y = y.size
n_sample = sample.size
time_step = 1/sr
freq_y = np.fft.fftfreq(n_y, time_step)                        # Discrete Fourier Transform frequencies of entire song
freq_sample = np.fft.fftfreq(n_sample, time_step)              # Discrete Fourier Transform frequencies of sample

print("y length: {0}, fft_y length: {1}, fft_sample length {2}".format(len(y), len(fft_y), len(fft_sample)))
# returns: y length: 6431544, fft_y length: 6431544, fft_sample length 58807

print("freq_y length: {0}, freq_sample length: {1}".format(len(freq_y), len(freq_sample)))
# returns: freq_y length: 6431544, freq_sample length: 58807

也许我做的不对,或者是我的方法不好。 谢谢你的关注,如果我在英语中犯了错误,请原谅!你知道吗


Tags: ofthesamplefftlensongnpposition
1条回答
网友
1楼 · 发布于 2024-04-25 17:32:25

从混合物中分离出不同的声源称为声源分离。如果您没有关于源的详细信息,则称之为盲源分离。这是一个总体上具有挑战性的问题,并且得到了广泛的研究。您可以尝试的一种简单方法是FastICA: https://scikit-learn.org/stable/auto_examples/decomposition/plot_ica_blind_source_separation.html

音乐尤其具有挑战性,因为这样的过程通常会产生小的人工制品。您可能需要稍后编辑这些内容。你知道吗

相关问题 更多 >