I want to plot real time original audio signal and fft signal.
my code is,
socket_server.bind(server_address)
print "Listening...\n"
while(True):
packet, client = socket_server.recvfrom(buffer_size)
count=count+1
if packet=='stop':
print "client stops to send packets"
break
#Convert packet to numpy array
signal.append(np.fromstring(packet, dtype=np.int16))
print "First Here"
socket_server.close()
signal_f = np.fft.fft(signal)
**Time = np.linspace(0,(len(signal)*(buffer_size/2))/sample_rate, num=(buffer_size/2))**
f, ax = plt.subplots(2, sharex=True)
ax[0].plot(Time, signal)
ax[0].set_title('Original Signal')
ax[1].plot(Time, abs(signal_f), 'r')
ax[1].set_title('fft signal')
plt.show()
I receive packets(which are 16 bit pcm data) from android app. and minbuffersize is 3584 bytes. (buffer_size = 3584)
To get time vector, I should divide size of signal by frame rate.
So in bold line, i think size of signal is len(signal)(# of received packet) * buffer size/2(1792 bytes -> size of packet received). is it wrong?
after check, calculated time is exact! I used stopwatch and compared the result of [(len(signal)*(buffer_size/2))/sample_rate]. both are the same.
but above code results error in plotting.
ax[0].plot(Time, signal) error is " x and y must have same first dimension"
what should i do?,,