Python can be a tremendous help when it comes to efficiently analyzing shock and vibration data, and there is a whole host of libraries which python can utilize to help streamline one's analysis.
Implementing these libraries can be intimidating to those unfamiliar with Python. In this post, we'll take a look at how to use the code of one of the open-source enDAQ libraries to filter shock and vibration data in the frequency spectrum that's important to your analysis.
We'll also break down what the code does, and discuss how to edit the code to make it useful for your data.
The first thing to do is copy and paste the usage example code into Google Colab (and click the new Notebook button); Colab is a free tool that you can use in your browser to run Python code.
I will do this in three sections with each section going into its own cell. Splitting the code up this way is unnecessary, but it will save me a few seconds if I decide to make changes later on and want to run the code again.
...
First Cell
Installs endaq library
Note: that the usage example does not include the first line to install the library. I had to add that line in myself. If you are running the code in a notebook locally on your computer and already have the library installed, this step is not necessary, but I need to do it every time because I’m using Colab.
!pip install endaq
...
Second Cell
Imports plotly and endaq libraries
import plotly.express as px
import endaq
...
...
Upload Data
Once our code is in, I’ll upload our data file to the notebook so that we can work with some real data. To upload the file, I just click on the upload button on the left side of my screen (circled in the screenshot above) and select the file I’m interested in.
Once I have the file uploaded, I can click the play buttons next to each box to run the code. That works great and gives me a nice chart as an output, but this is just sample data. How do I get this to use my data? Let’s go through the code and see what edits I need to make to get this to work with some other data.
The first two sections of this code are only here to install and import the libraries we’ll be using. We don’t need to change anything here and we can just move on.
The next section of the code is where we’ll need to make some changes, so I’ll go line by line to demmonstrate what everything does.
The portion in green is comment text - it tells us what is happening in this line. The comment doesn’t impact the code being run, but it is useful to guide us as we work through the code. This line tells the code what file we want to look at and specifically which channel of data in that file.
I’m going to delete that URL and replace it with the name of the file that I uploaded earlier. It is important that I only change the file name and I leave the apostrophes on either side of the file name intact. The channels, subchannels, and time_mode portions of this line are also important. In the original file used in the example before I started making changes, channel 8, subchannel 2 corresponded with the z axis of the primary accelerometer.
For my file, I want to look at the x-axis of my secondary accelerometer, so I’m going to change those numbers to channel 80 subchannel 0. The time_mode option will control how the timestamps of your chart are presented. I’m happy to leave it in seconds, but this could be changed to datetime if you want to see the exact date and time of your recording. I don’t need to change the green comment but I will make an edit so I don’t confuse myself later.
NOTE: If you don’t know the channel or subchannel of the accelerometer you want to look at, you can find that information in the enDAQ Lab software by opening the file and pressing ctrl+i to bring up the recording properties menu. If you click on the channel info tab in that menu, you’ll see the channel numbers. If you want some more Python practice, we have another usage example that will pull up the channel table so you can view the channel numbers right in your Python notebook
After I’ve made those changes, here is my new line of code
To better understand what is going on with the next few lines, let’s work backwards and start with our output. The output of this code, as it is currently written, is going to be 3 lines plotted on a chart.
We already have our raw data, so the next two sections of the code are going to generate the lines with the filters applied.
Again, the green part here is just a comment. The first real line of code here is applying the 1 Hz filter. We can change that frequency by adjusting the number after low_cutoff. For the sake of being different, I’m going to set this to 5 Hz. The next line in this section is just adding a title, so this line will be accurately labeled when it is plotted. Here’s my new line of code with the changes made.
The next section of code is going to look very similar to the last section because it’s just adding a low-pass filter instead of a high-pass filter.
I’m going to change this to 50 Hz instead of 100 Hz and to do that, I’m going to change the same values I did last time. The only difference is that I’m going change the high_cutoff value instead of the low_cutoff this time.
NOTE: There’s nothing stopping you from adding a high and low pass filter to the same line. You can change the low_cutoff and high_cutoff frequencies on the same line if you want to add both filters together rather than seeing them plotted separately.
The remainder of the code will merge all three of these lines into one dataset and then plot them in a chart. We don’t need to change anything else here and we can just press play to let this code run and it will give me a nice plot of my data, filtered exactly how I want it.
In conclusion, I have a nice interactive chart of my data that I have customized to my liking and all I had to was copy and paste some existing code with extremely minor changes to adjust things the way I want. Our Python Library may look intimidating if you’re new to coding, but we’ve already done the hard part for you and it only takes some small adjustments to completely customize this code for yourself.
Check out some of our other Python Related Posts.