首页 > 解决方案 > 检查数组中每个元素的每个值是否包含在另一个数组中 - python

问题描述

我是 python 新手,但我有一个项目来创建一个使用 python 的程序。该程序的概念是将录制的声音分类为“a 类”和“b 类”。我尝试使用这个概念,因为录制的声音的结果是太多的噪音,因为噪音变化(不可预测的噪音)而无法避免并且无法被过滤。

所以首先,我有一个“声音样本”作为参考。我把“声音的样本”变成了时域的频率形式(如频谱图),我们可以称之为“声音样本”。然后我记录声音,该声音也被转换为时域中的频率形式(与“样本声音”形式相同)。我们可以称之为“测试声音”。

它们(“声音样本”和“测试声音”)都是多维数组的形式,然后我想检查“样本声音”数组中每个元素中的每个值是否包含在“测试声音”中”。

现在,问题来了,如何检查“样本声音”数组的某些元素中的值是否包含在“测试声音”数组中?...这是我使用的代码。

import wave
import struct
import sys
import numpy as np

def wav_to_floats(wave_file):
w = wave.open('....wav') #>>opening wav file of "the sample sound" 
astr = w.readframes(w.getnframes())

a = struct.unpack("%ih" % (w.getnframes()* w.getnchannels()), astr)
a = [float(val) for val in a]

return a

the_sample_sound =  np.array(wav_to_floats(sys.argv[0]))

def wav_to_floats_2 (wav_file):
v = wave.open('....wav') #opening wav file of "the testing sound"
astu = v.readframes(v.getnframes())

b = struct.unpack("%ih" % (v.getnframes()* v.getnchannels()), astu)
b = [float(val) for val in b]

return b

the_testing_sound = np.array(wav_to_floats_2(sys.argv[0]))

time = 30 #duration time of sound (both "the sample sound" and "the testing sound") is 30 second

c = np.array_split(the_sample_sound, time)

for i in range (time) :
d = np.abs(np.fft.fft(c[i]))

e = np.array_split(the_testing_sound, time)

for j in range (time) :
f = np.abs(np.fft.fft(e[i]))

# i tried to use this block code to check value from the sample sound to the testing sound but it could not work

h = 0 

for i in (d) :
    for j in (e) :
        for u in (j) :
            if u in (i) :
                h += 1
            else :
                h += 0

print (h) #>> to sum how many times value in sample sound is contained in the testing sound

附加信息我使用python 3x,抱歉英语不好..

标签: pythonarraysnumpyaudio

解决方案


推荐阅读