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:
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?