How to make a GUI using PyQt5 and Matplotlib to plot real-time data: PyQt5 tutorial - Part 10

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello friends and welcome to pie shine this is part 10 of the pie qt5 learning series today we will design a graphical user interface in python to plot the live audio data we will learn how to put a matplotlib figure to pi qt5 gui [Music] also we will talk about the basics behind digital audio [Music] a sound converted to digital form represents the digital audio first the sound is sampled at equal intervals using an analog to digital converter for example let's assume we have taken five samples of the audio waveform as shown these samples are decimal numbers and in five bits binary each of them can be represented as shown this binary data is then sent to let's say a 5-bit digital to analog converter to regenerate the wave format speakers this waveform should be sampled at a sufficiently high frequency to approximately represent the sound today we will use these decimal numbers to plot the live audio from the microphone of computer in pi qt5 gui [Music] the audible sound has a frequency range from 20 to 20 000 hertz and the fundamental frequencies of the typical adults are only a few hundred hertz according to the nyquist criteria the sampling frequency should be at least twice the fundamental frequency to properly sample the audio waveform it means that a sampling frequency or sample rate of 40 kilohertz is enough to capture the audible frequency of 20 kilohertz however in general it is sampled at 44.1 kilohertz this sampling rate originated in the late 1970s with pcm adapters to record the digital audio on video cassettes such as the sony pc 1600 the rate was chosen by sony and has become a de facto standard because this suits well for pal and ntsc video standards to plot the audio data we should consider a set of parameters so let's start making the gui which will input these parameters to plot let's create a new main window to hold the set of parameters we require a group box so we drag and drop it on the main window from the container widgets all right let's change its name to parameters at the top we can give a title using the label taken from the display widget [Music] now we can change the font size from the property editor as shown [Music] also we align it center to the horizontal let's put the combo box that will have a list of audio devices also we add a label beside it [Music] the length of audio data is given by the parameter window length it will take value from the line edit which is an input widget similarly the sampling rate parameter can be added to skip some samples we can select the value of down samples if it is 10 that will mean to drop 10 previous samples in the sequence of data to show the plots in real time it should be updated at a specific interval this is called update interval and it is in milliseconds the last one in this group box is a push button that will let us to start the plot all right let's give them the grid layouts to keep things nice and tidy [Music] we will now put a widget to hold the place where the matplotlib figure will finally show up let's give it a black background to keep the gui intact let's add the horizontal and vertical spaces do let's rename the push button and add the initial values to the parameters also we can write units to some of the labels [Music] we can remove the additional spacing added by these spaces by setting their width or high to zero as shown now we can save this as let's have a look at it same as our previous tutorials here we'll convert it to main dot pi file but this time we will only use it as a reference to the location of the widget where we can put the plot figure [Music] also we can easily find out the line edit number for the corresponding parameter [Music] all right we will make the gui dot pi file to load the main.ui file this is a better approach to design guis in pi qt5 because in this way you can edit the ui file without changing the gui dot pi file let's import the essentials the sound device module will be used to obtain the audio data from the microphone uic will be used to load the main.ui the input audio device infos will be used to get the list of audio devices in computer here we make the class for the matplotlib canvas figure now we will make the class to represent the main gui window here we assign the user interface from the main.ui file resize the main window after adding the icon we call the thread pull from the q thread pool make a devices list and use it in the combo box whenever the selection in combo box changes we will call the self dot update now from the main dot pi file we observed that the position of widget is 2 1 1 1 in the grid layout form so here we will add the figure canvas instead of that widget we initialize the reference plot to none for the start the queue will be used to buffer the audio data the size of q is 20. the audio device index is zero the parameters are initialized here here we give the default sample rate to the sound device we will use qt timer to keep on updating the plot at the desired interval from main dot pi file we know each line edit and its relevant parameter name for each of them we will call the respective function a worker q thread will be started when the push button is pressed the get audio function will start the audio stream put the audio data into the queue [Music] now the start worker function will start the thread as a worker this worker class will be discussed shortly it will call the start stream function to disable all input parameters and start the audio stream by calling the get audio function let's define functions for the parameters [Music] here in the update plot we will get a data from the queue using the numpy we will roll it and plot it [Music] the cue run able class is well suited to situations where we want to perform some background processing in one or more secondary threads without needing the full power and flexibility provided by cue thread at pike slot is a decorator which converts simple python method to qt slot this has the advantage of reducing the amount of memory used and is slightly faster you can easily visualize the performance difference by calling the normal thread instead of cue thread [Music] first we will let the gui run as it is next let's change the window length to 5000 it will increase the number of samples to be displayed [Music] increasing the update interval to 1000 milliseconds will be like refreshing data every one second you can play around with the sampling rate as well that's all for today if you have question suggestions please comment share if you like and subscribe to pi shine have a nice day and see you again
Info
Channel: PyShine
Views: 18,509
Rating: undefined out of 5
Keywords:
Id: Ng00Mj5Tt8o
Channel Id: undefined
Length: 19min 15sec (1155 seconds)
Published: Fri Nov 13 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.