Lab 3 - Processing and Analyzing Data (1)

pdf

School

Oregon State University, Corvallis *

*We aren’t endorsed by this school

Course

351

Subject

Mechanical Engineering

Date

Dec 6, 2023

Type

pdf

Pages

10

Uploaded by SuperRose6727

Report
Page 1 of 10 Lab 3 - Processing and Analyzing Data ME 351 - Introduction to Instrumentation and Measurement Systems Lab Days: 2 What we’ll learn: How to do frequency analysis MATLAB FFT package How to filter data Moving average filter (software low-pass filter) MATLAB low-pass and high-pass Signoffs Email all signoff videos to: lab_3_s.8rz5dzg2xv47xnkb@u.box.com Part 1: Finding Trends in Data Before, we’ve extracted raw data from sensors. But oftentimes, we’re more interested in trends than each data point. For instance, data may be regularly repeating, and we may want to know how frequently it repeats. We can try to do so by determining how many times it peaks during a set time. Tasks 1. Open your MATLAB plotting script for your photocell data from Lab 2. 2. Write a program using the findpeaks() function built into MATLAB ( mathworks.com/help/signal/ref/findpeaks.html ) that counts the number of times your data “peaks”, or has a high po int. a. Note: You do not need to use the additional options (e.g “MinPeakDistance”). The most basic version of findpeaks() is fine. 3. Plot the position of the peaks your program identified (using the scatter() function) along with your raw data (using the plot() function). 4. Find and record the average frequency of the strobe light used in the last lab according to your program. a. Using the [pks, locs] results from findpeaks() helps with this.
Page 2 of 10 Discussion Question 1 Does the most basic version of findpeaks() tend to overestimate or underestimate the frequency of the signal (the trend in the data)? Why do you think this is? Tasks, Continued Another way to find trends is to analyze data to try to approximate it as waves and see if they fit well - this is called frequency analysis, and we can do it in MATLAB using fast Fourier transforms, or fft() ( mathworks.com/help/matlab/ref/fft.html ). 5. Go to the MATLAB help page for the fast Fourier transform function. 6. Using the “Noisy Signal” example, analyze your own photocell data with fft() . a. Keep in mind that the example generates data for analysis while we already have data to analyze. Make sure you are running the analysis on your own data! b. The initial parameters must also be changed to fit our data. You can determine the real sampling frequency (Fs) by finding the average sampling period (difference between timestamps) and dividing 1 by it. Remember to convert the period from milliseconds to seconds first. i. Note: Sampling frequency (Fs) is not a time difference. It is a frequency with units of Hz. If your FFT plot looks strange, the first place you should check to debug is your value for Fs. 7. Plot the single-sided spectrum analysis (P1) with respect to frequency in Hz. 8. Change the x-axis limits of your plot if necessary to ensure the frequency value associated with the peak is obvious to those looking at your graph. 9. Find and record the frequency of the light according to the FFT analysis. 10. Combine your peak-finding graph and your FFT graph as subplots in the same MATLAB figure ( mathworks.com/help/matlab/ref/subplot.html ). 11. Show your figure to a teacher for sign-off 1. 12. Alternatively, save your figure and submit it via email for sign-off 1. a. DO NOT take a screenshot of the figure. Save the plot programmatically with print or saveas , or, in the figure window, use [File] → [Save as]. b. Save the file as an .jpg, .jpeg., .png, or .pdf file and title it Firstname_Lastname-L3- S3”. The file must be smaller than 25 MB. c. Email the file to the box account above. You should receive a confirmation email that the file was uploaded successfully.
Page 3 of 10 13. Make a copy of your MATLAB code for your lab report. Discussion Question 2 Include the plot of your most basic version of findpeaks() and your fft() plot generated in the above tasks. What frequencies did you find for peak-finding and for FFT? How do your frequencies compare between peak-finding and FFT? If they are different, which one do you think is more accurate and why? You may find it helpful to do some simple visual analysis of your data. Keep in mind the formatting instructions on the template for figures. Discussion Question 3 In your FFT plot, you might see that there is a large peak at 0Hz. What does this peak represent? Hint: If a wave has a frequency of 0, does it oscillate or is it constant? Part 2: Filtering A Noisy Sensor Filtering is particularly helpful for noisy sensors. One very common noisy sensor is an inertial measurement unit (IMU) - a sensor that can measure some aspects of the motion it’s experiencing. Since our kits don’t come with an IMU, we have provided noisy data from an IMU on Canvas. We’re using the MPU -6050, an often-used IMU with an accelerometer and gyroscope in it. Because of its popularity, there are several guides and libraries for its use. If you need to get your own sensors for a project in the future, be sure to search for guides and libraries before you buy! Planning ahead like this can save you quite a lot of headache. Tasks 1. Go to this Adafruit MPU-6050 guide to learn about the sensor used to collect this data: learn.adafruit.com/mpu6050-6-dof-accelerometer-and-gyro/overview 2. Watch this video to learn how an accelerometer works: youtu.be/KZVgKu6v808 Discussion Question 4 How does a 1-axis accelerometer work? How might an accelerometer measure in 3 axes? How many axes does the MPU-6050 measure in? Tasks, Continued Noise can confuse our analyses, since they have to separate the real data (signal) from the random errors in the data (noise). The more random error, the noisier the data is. We can alleviate the effects of noise by filtering the data before we analyze it. One of the
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Page 4 of 10 easiest ways to do so is to add a real-time moving average filter. Real-time filters are also useful for when your system acts on incoming data and is sensitive to noise. If you want your system to behave appropriately, you will often have to implement real-time filtering to avoid acting on noise, rather than the intended signal. An Arduino example for a real-time moving average filter can be found by going to your Arduino IDE and opening [File] → [Examples] → [03.Analog] → [Smoothing]. You can use this to smooth noisy data from any sensor you can read data from (keep in mind for final project). Since you do not have access to an IMU for the Arduino, we will not be using a real-time moving average filter. Instead, you will clean the data in post-processing by using pre- recorded data and running a moving average filter on it in MATLAB. The recorded data is from flipping the IMU around the Z-axis in time with a metronome set to 90 BPM ( https://youtu.be/I7mFvUl9HjA ). Tasks 1. Download the pre- recorded IMU data “imu_flipping_data.csv” from Canvas. a. The first column is time in milliseconds and the second column is the acceleration read by the z-axis accelerometer. 2. Download th e starter code “moving_avg_filter_starter_code.m from Canvas. 3. Read carefully through the provided code to understand how a moving average filter works. 4. To compare the effects of window size on our data, filter the raw data with window sizes 10, 50, and 100. 5. Plot your filtered signal at each window size on a single subplot (raw, 10, 50, 100). Run an FFT analysis on each set of data (4 in total), and plot each individual FFT analysis as a subplot under the combined plot of raw and filtered data. (The result should be five vertically stacked subplots.) a. Note: If your FFT plots don’t look right, make sure you convert from milliseconds to seconds and that your Fs value is appropriate. 6. Show your figure to a teacher for sign-off 2. 7. Alternatively, save your figure and submit it via email for sign-off 2. a. DO NOT take a screenshot of the figure. Save the plot programmatically with print or saveas , or, in the figure window, use [File] → [Save as].
Page 5 of 10 b. Save the file as an .jpg, .jpeg., .png, or .pdf file and title it Firstname_Lastname-L3- S3”. The file must be smaller than 25 MB. c. Email the file to the box account above. You should receive a confirmation email that the file was uploaded successfully. 8. Make a copy of your MATLAB code for your lab report. Discussion Question 5 Include your plots for this section in your lab report. Compared to the raw data, briefly describe the filtered data’s characteristics. Is it faster/slower? More/less noisy? What effects does the filtering have on the results of the FFT analysis? Discussion Question 6 What is the relationship of sample rate to window size for a moving average filter? If you had a moving average filter with a window size of 10 and wanted a similar filtering effect while halving your sample rate, what should your new window size be? Part 3: Post-Processing Data in MATLAB The moving average filter can help, but doesn’t cover all of the possibilities when filtering. We can try out the effects of more capable filters (e.g low-pass and high-pass filters) when post-processing data in MATLAB. To make our lives a little easier, we’ll be using a community -created package called filter1 ( mathworks.com/matlabcentral/fileexchange/53534-filter1 ). When you need to code something and you think someone else may have done it before, Googling for these kinds of packages can save you some time. On the other hand, it may not be as reliable as official MATLAB packages and you’ll need to learn how to use it. Quick tips : You’ll need at least MATLAB 2016 to be able to use some of the functions. If this is an issue, you should be able to use matlab.mathworks.com to run your code instead. Headphones might also be useful as the low-pass filter can be hard to hear. Tasks 1. Go to the Examples page on the filter1 documentation page above. Read through the first example up until the third plot (low-pass filtering). 2. Create a MATLAB script that: a. Loads t he music file “song_original.wav” provided on Canvas. Use audioread() to read in the song’s signal and sampling frequency.
Page 6 of 10 b. Uses filter1() first as a low-pass filter to filter out high frequency percussion noises and leaves the rest of the music alone. i. Use the instrument_frequency.jpg spectrum chart on Canvas to choose an appropriate cutoff frequency for a low-pass filter. ii. As the chart suggests, instruments may cover a broad range of frequencies. Do your best and try several cutoff frequencies, but don’t o bsess over all of the percussion noises! c. Plays the filtered music. Use sound() . i. To stop the music playing early, use clear sound . ii. You can play the music for a set time by using: pause(x seconds) clear sound d. Writes the filtered music to a new .wav file. i. Use audiowrite(‘name_of_new_file.wav’,... ...filtered_signal, Fs) e. Save the low-pass filtered music as a .wav file for sign-off 3. f. Submit your file via email for sign-off 3. i. DO NOT take a screenshot of the figure. Save the plot programmatically with print or saveas , or, in the figure window, use [File] → [Save as]. ii. Save the file as an .jpg, .jpeg., .png, or .pdf file and title it Firstname_Lastname-L3- S3”. The file must be smaller than 25 MB. iii. Email the file to the box account above. You should receive a confirmation email that the file was uploaded successfully. 3. Now that we’ve used a low -pass filter to remove high frequency percussion, let's try removing the low frequency bass instruments with a high-pass filter on the original sound file (do not run the high-pass filter on your low-pass filtered file!). a. Run the same code, just change your filter type and your cutoff frequency to a more appropriate value for a high-pass filter. b. Save the high-pass filtered music as a .wav file for sign-off 4. c. Submit your file via email for sign-off 4. i. DO NOT take a screenshot of the figure. Save the plot programmatically with print or saveas , or, in the figure window, use [File] → [Save as].
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Page 7 of 10 ii. Save the file as an .jpg, .jpeg., .png, or .pdf file and title it “Firstname_Lastname -L3-S4 ”. The file must be smaller than 25 MB. iii. Email the file to the box account above. You should receive a confirmation email that the file was uploaded successfully. 4. Let’s downsample our music to a sixth of the sample frequency. We can do this with downsample() . Listen to the changes in the music. a. Downsampling does not speed up the playback of the song. Make sure when you call the sound() function, you adjust the Fs you call in the playback argument to reflect the downsampled signal. b. Save the 6-times downsampled music as a .wav file for sign-off 5. c. Submit your file via email for sign-off 5. i. DO NOT take a screenshot of the figure. Save the plot programmatically with print or saveas , or, in the figure window, use [File] → [Save as]. ii. Save the file as an .jpg, .jpeg., .png, or .pdf file and title it “Firstname_Lastname -L3-S4 ”. The file must be smaller than 25 MB. iii. Email the file to the box account above. You should receive a confirmation email that the file was uploaded successfully. 5. Make a copy of your MATLAB code for your lab report. Discussion Question 7 Describe the effects of the low-pass and high-pass filtering on the original music, as well as the effects of downsampling on the music you hear. Discussion Question 8 Run FFT analyses on the original, low-pass filtered, and high-pass filtered music. Show your three resulting plots (in three vertically stacked subplots) and report the cutoff frequency you used for each filter (in the plot or in your answer text). What effects does filtering have on the FFT results? Extra Credit: Noise Filtering Filtering noise to isolate the desired signal is an open problem in engineering. We have provided two real-world sound files recorded from a robotic stand-up comedian, in which the desired signal is buried within a lot of noise. Basic filtering techniques, as used in this lab, can be helpful in cleaning up some of the unwanted noise found in sound samples.
Page 8 of 10 Tasks 6. Download “joke_0.wav” and “laughter_5.wav” from the Lab 3 Canvas folder. 7. Run an FFT analysis on the sound files to determine the frequencies at which the desired signal is found, and which frequencies are noise 8. Use the bandpass option on filter1 to try and isolate the joke in “joke_0.wav” and the laughter in the “laughter_5.wav” from the noise. 9. There is no known solution, as cleaning noise from these files is an open problem, so do your best to isolate the desired signals, but you may not be able to get rid of all the noise. 10. Feel free to try filtering both files, but only one should be submitted for extra credit. 11. Save your best filtered .wav file for the optional sign-off 6. 12. Play your .wav file for an instructor for sign-off 6.
Page 9 of 10 Post Lab Questions 1. Music Filtering a. What kind of data does a .wav contain? What is different about how the .mp3 file holds data, and how do we make one from a .wav file? b. What is the importance of sampling rate? What is the Nyquist frequency? For music, what are some common sampling rates and how do they compare to audible frequencies? c. Why is there such a difference between the downsampled and original music?
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Page 10 of 10 Lab 3 Sign-Offs Page This page is a central reference for the sign-offs you must complete during the lab. Make sure to demonstrate the following items to a lab instructor as you complete the lab. They will document your completion of each sign-off in the class logs. Sign-offs for Lab 3: 1. Comparing findpeaks() to fft() . 2. Creating a moving average filter and analyzing it. 3. Filtering music (low-pass). 4. Filtering music (high-pass). 5. Filtering music (downsampled). 6. OPTIONAL Noise filtering (extra credit)