CR555
Published

Oh! Nice spectrum!

Recognize what material an object is made of by simply touching it

IntermediateWork in progressOver 3 days291
Oh! Nice spectrum!

Things used in this project

Hardware components

Ultra96-V2
Avnet Ultra96-V2
×1
USB-A to Micro-USB Cable
USB-A to Micro-USB Cable
×1
Avnet External 96Boards Compliant Power Supply Kit
×1
Ultra96 JTAG/UART Pod
Avnet Ultra96 JTAG/UART Pod
×1
micro sd 16 GB
×1

Software apps and online services

Xilinx PetaLinux 2019.2
Putty
Visual Studio 2017
Microsoft Visual Studio 2017

Story

Read more

Schematics

Project block diagram

Code

Configure WiFi

Plain text
You have to modify wpa_supplicant.conf in the root directory
ctrl_interface=/var/run/wpa_supplicant
ctrl_interface_group=0
update_config=1

network={
        key_mgmt=WPA-PSK
        ssid="**********"
        psk="**********"
}

webserver.py

Python
these are the last lines of the webserver.py file after we have entered the webapp named music.html.
To view your app you will need to delete or at least comment the two lines that correspond to lines 15 and 16 so that the code corresponding to the page we created is executed.
.............
.............
.............
def check_connection():
    p = subprocess.Popen("iwgetid", stdout=subprocess.PIPE, shell=True)
    ssid_output = p.communicate()[0]
    if ssid_output != "":
        return "true"
    return "false"

#CUSTOM USER BACK END CODE ADDED AFTER HERE

#CUSTOM USER BACK END CODE STOPS HERE

#if __name__ == '__main__':
#    app.run(host='0.0.0.0', port=80, threaded=True)

#CUSTOM USER BACK END CODE ADDED AFTER HERE

#The following will briefly explain how to edit code on this page
#For more information go to the Tutorial page and select the "Custom Back End" $
#Make sure to change "CHANGE ME" to be the name that was given to the file on t$
#Good luck and have fun!

@app.route("/spettro.html", methods=["GET", "POST"])
def spettro():
    if request.method == "POST":
        return render_template("CustomContent/custom_front_end/spettro.html$
    else:
        return render_template("CustomContent/custom_front_end/spettro.html$
#CUSTOM USER BACK END CODE STOPS HERE

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=80, threaded=True)

spectrum.py

Python
This is the code that receives the .wav file as input, finds its power spectral density and provides as output the array of spectral components which will then be the inputs of the classifier.
import wave
import matplotlib.mlab as mlab 
import numpy as np
import matplotlib.pyplot as plt

#normalizza le componenti spettrali (min=0, max=1)
def normalizza(v):
    #cercamin
    minimo=np.min(v)
    #elimino offset
    uno=np.ones(len(v))
    offset=v-uno*minimo
    #calcolo massimo di offset
    massimo=np.max(offset)
    normale=offset/massimo
    return normale


n=10    #n° di componenti spettrali che voglio considerare
vettore=np.ones(n)

# Carico il file wav
signal_wave = wave.open('/usr/share/ultra96-startup-pages/webapp/templates/CustomContent/uploaded_files/Guitar.wav', 'r')
sample_rate = signal_wave.getframerate()    #frequenza di campionamento
sig = np.frombuffer(signal_wave.readframes(sample_rate), dtype='int16')

sig = sig[:]

Length = len(sig)   
numUniquePoints = np.ceil((Length + 1) / 2.0)
numUniquePoints = int(numUniquePoints)

#Calcolo la densità spettrale di potenza
p = np.fft.fftn(sig)
p = p[0:44100:1]
p = abs(p)
p = p / float(Length)
p = p **2


if Length % 2 > 0: #we've got odd number of points in fft
    p[1:len(p)] = p[1:len(p)] * 2

else: #We've got even number of points in fft
    p[1:len(p) -1] = p[1:len(p) -1] * 2  

freqArray = np.arange(0, numUniquePoints-1, 1.0) * (sample_rate / Length);

#Calcolo il vettore delle componenti spettali
num = int(len(p)/(2*n))
for j in range(0,n):
    risultato = 0
    for i in range(1, int(num/2)):
        risultato = risultato + 10*np.log10(p[j*num+i])
    vettore[j]=risultato/num

vettore=normalizza(vettore)

#creo figura
fig = plt.figure()

plot_a = plt.subplot(221)
plot_a.plot(sig)
plot_a.set_xlabel('Sample')
plot_a.set_ylabel('Amplitude')

plot_b = plt.subplot(222)
plot_b.specgram(sig, NFFT=1024, Fs=sample_rate, noverlap=900)
plot_b.set_xlabel('Time')
plot_b.set_ylabel('Frequency')

plot_c = plt.subplot(223)
plot_c.plot(freqArray[0:10000]/1000, 10 * np.log10 (p[0:10000]), color='b')
plot_c.set_xlabel('Frequency (Khz)')
plot_c.set_ylabel('Power (dB)')

plot_d = plt.subplot(224)
y_pos = np.arange(n)
plt.bar(y_pos, vettore, align='center', alpha=0.5)
plt.xticks(y_pos, y_pos)
plt.ylabel('Power')
plt.title('')


plt.show()

#save the plot to an image file to display in the webapp
fig.savefig('/usr/share/ultra96-startup-pages/webapp/static/images/output.png')

#save array components in txt file
np.savetxt('/usr/share/ultra96-startup-pages/webapp/static/images/Data.txt', vettore, fmt='%6.3f')

Credits

CR555

CR555

2 projects • 5 followers

Comments