-1

I'm trying to plot a selected number of samples after reading a wave file. I wrote the following code to achieve that:

import numpy as np
import matplotlib.pyplot as plt
from scipy.io.wavfile import read

(fs, x) = read('/home/sk_he/sounds/sample.wav')

M = 501
start_time = 0.2
start_sample = int(start_time * fs)
stop_sample = int(start_time * fs) + M
x1 = x[start_sample:stop_sample]
stop_time = float(stop_sample/fs)
tx1 = np.linspace(start_time, stop_time, M)
plt.plot(tx1, x1)

This gives me the following output: Plot

Though this is fine, I intended to indicate the time from 0.2 s to whatever time M samples end. I have also given the start and stop values correctly to linspace. But the plot still has the first value as 0.0 instead of 0.2. How do I fix this error in the above code so that it rightly starts at 0.2 instead of 0.0 on the x-axis?

skrowten_hermit
  • 437
  • 3
  • 11
  • 28

1 Answers1

0

The problem lies where the type casting is done. I've modified the code and it displays the output just as intended:

start_time = 0.2
start_sample = start_time * fs
stop_sample = (start_time * fs) + M
x1 = x[int(start_sample):int(stop_sample)]
stop_time = float(stop_sample/fs)
tx1 = np.linspace(start_time, stop_time, M)

The following graph is the correct expected output:

CorrectPlot

skrowten_hermit
  • 437
  • 3
  • 11
  • 28