1

in the following piece of code I've extracted a window of data off of an audio sample(1000Hz signal). In the code, I've tried to obtain a wavelength of the signal. https://paste.pound-python.org/show/HRVqQNy3w9Sr73q4oY8g/

sample = data[100:200]
x = 0
i = 1
num_occur = 0
while num_occur <2:
    if sample[i] == sample[0]:
        x = i
        i += 1
        num_occur += 1
    else:
        i += 1
wavelen = sample[:x]

But with less success... the image of the sample : (https://pasteboard.co/HFFXGxW.png)

Well, I do understand what the problem is; even though matplotlib plots the wave as a continuous wave(due to the high sampling frequency), the wave is made up of discrete data, so there may or may not be a data value satisfying:

sample[i] == sample[0]

I'll greatly appreciate any help and advice on how to get around this problem.

  • Hi! Welcome to StackOverflow! Given that the wavelength is given by the distance of two consecutive peaks, this might be a possible duplicate of https://stackoverflow.com/questions/1713335/peak-finding-algorithm-for-python-scipy/ – norok2 Sep 26 '18 at 13:59
  • Hey, thank you! yes, that is probably a good suggestion, but here in my case, the answer below works just right. But I guess if I were to analyze more complex signals (normal speech signals) I would have to use the distance between peaks as mentioned. – Naveen Devinda Sep 27 '18 at 04:36

1 Answers1

0

Someone enlightened me on how to get to the answer. It's a simple but logical approach. So I just needed to extract the points that would cut 0, and the wave between the 1st and 3rd such consecutive points would give me a wavelength.

Here's the code I wrote:

sample = data[100:200]
i = 1
num_occur = 0
cut_zero = []
x = 0
while num_occur < 3:
    if sample[x] == abs(sample[x]):
        if sample[i] == abs(sample[i]):
            i +=1
    else:
        cut_zero.append(i)
        num_occur += 1
        i += 1
        x = i
    elif sample[x] != abs(sample[x]):
        if sample[i] == abs(sample[i]):
            cut_zero.append(i)
            i += 1
            num_occur += 1
            x = i
        else:
            i += 1
print(cut_zero)
a = cut_zero[0]
b = cut_zero[2]
wavelen = sample[a:b]

Maybe I could do it more efficiently :), if so let me know. here's the image of the wavelength https://pasteboard.co/HFLtOmr.png