TMMS04 1 Computer Lesson – Signal Processing 2014 HT-1 Signal Processing Computer Tasks The primary purpose of this computer lesson and hand in assignments is to give you an introduction to basic signal processing requirements. Since this is not a course in signal processing the tasks should not require any deeper knowledge about signal theory. The secondary purpose is to introduce you to (or refresh your skills in) Matlab programming. Knowing how to write scripts to automate data analysis and visualization is very important when developing Mechatronic systems. These skills are also useful in many other fields. In this course the Matlab program and scripting language is used but many other tools and languages with similar capabilities exists. 1.1 Questions The tasks are divided into two parts. In the first common part all student will process the same data. This parts is meant to introduce the basics that you need to know. You should solve the questions in the given order as sometimes it will assume that you have completed an earlier task. In the second individual part each student will get a personal data set to work with. This means that you will/may not get the same results as your friends. These questions require you to figure out for your self what you need to do, but the solution will be based on things that you have already done in the common questions. 1.2 Report You have to hand in ONE zip-file per e-mail to mailto:martin.hochwallner@liu.se with the subject “TMMS04 Mechatronics – Computer Lesson – Signal Processing”. This zip-file’s name shall starts with your LiU-ID. This zip-file shall contain all files you want to hand in. 1.2.1 Deadline Hands-in arriving after this deadline will not be recognized for grading. common tasks 2014-09-21, So, 24:00 common tasks resubmit 06:00, 48h after receiving feedback for the previous attempt individual tasks 2014-09-28, So, 24:00 1.2.2 Files You should hand in multiple files of the following types. Written reports (pdf) This should contain your answers to the questions, figures, comments and motivations when necessary. Do not include your m-script code here. Figures should preferably be in the .png format (or a vectorgraphics format if supported). Do not use .jpeg, that format is for photographs and will make figures noisy and ugly unless the resolution is very high. There should be one report for the common part and an other for the individual part. Important! Name your reports liuID_common.pdf and liuID_individual.pdf, where liuID is your liuId. Also make sure that the report contains your name, personal number and email address. m-script files The code to solve all common questions in one file. The code to solve all individual question in an other. Use sections %% do divide between each task and name the section with the task number. When we run your script it must work “out of the box” and produce the desired results. Important! Name your script files liuID_common.m and liuID_individual.m, where liuID is 1 TMMS04 Computer Lesson – Signal Processing 2014 HT-1 your liuId. At the top of each script file write a comment header containing your name personal number and email. % % % % Name : P . Num : LiU - ID : Email : Peter Nordin ****** -**** ******** peter . nordin@liu . se %% clc ; close all ; clear ; ... % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % + Task 1 fprintf ( ’\ n + Task 1\ n ’) % % ++ Task 1 a fprintf ( ’\ n ++ Task 1 a \ n ’) ... Additional files All additional files that your script requires to run, such as data files or any custom functions you have created. 1.3 Grading This task in it self will be graded pass or fail, but it will be included as a part of total grade for the course. To pass the assignment you must solve all of the Common questions. If you fail, you will be allowed to re-submit your answers, at most one time. The number of re-submits will affect the final grade negatively. You will only be allowed to hand in the individual questions once. How well you do, will affect the final grade for the course. In some questions there is a correct answer, and you must find it. In others it is up to you to choose what you think is best. In these cases a motivation for your choice is required as it will make it possible to judge your solution. 1.4 Matlab command cheat sheet For your convenience the Flumes Short Introduction to MATLAB 1 gives you a necessary introduction and examples to Matlab commands. You should read it! 1 http://www.iei.liu.se/flumes/guides?l=en 2 TMMS04 2 Computer Lesson – Signal Processing 2014 HT-1 Common Questions Task 1: Sampling Plot the original data vs. down-sampled versions. When plotting the down-sampled versions, be sure to highlight each sample. Ex: plot(t, y, ’*-’); Load the data file SPcommon.mat (a) What is the original frequency of the sine wave given by SP.samp.t and SP.samp.y1 ? (b) Sample the signal using downsample at the lowest sample frequency allowed by the sampling theorem and plot the result together with the original curve. Does the resulting sampled version represent the original well? Why? (c) Plot the sampled version of the curve based on a suitable sample frequency according to the “rule of thumb” FS = 10 F0 . Is the result better then before? (d) It is easy to be fooled by the automatic interpolation of the plot command. Try to use stairs(t, y) instead. This will show you the actual sample and hold output. An other way to plot sampled data is stem(t, y). (e) Now plot the curve given by SP.samp.t and SP.samp.y2 ; It is the same curve as in y1, but with an additional superimposed much higher frequency signal (close to 100Hz). Sample y2 at 100Hz. The resulting sampled curve will not look as the original y2, why not? (f) Increase the sample frequency until you think that the output looks “reasonably OK”. At what frequency does this happen ? Task 2: Analog First-order Continuous-time Filter Design Using the control-toolbox in Matlab, you can easily create transfer functions by writing their analytic expressions as a function of s. Begin by defining s = tf(’s’). (a) Create a first-order low-pass filter with corner frequency 100Hz. (b) Create a first-order high-pass filter with corner frequency 50Hz. (c) Create a band-pass filter by multiplying the Lp and Hp filters. (d) Plot the bode diagrams for the three filters using the bodeplot command. Plot them all in the same diagram but with different colors. Note! If you want to change the plot properties, have a look at bodeoptions. (e) Comment on asymptotic approximation of these Bode plots. Hint: Be aware of the logarithmic scaling in the Bode plot. (f) Comment on the pass-band amplification for the three filter types. What happens with the band-pass filter pass-band flatness when the low and high corner frequencies are moved further apart? Task 3: Contentious-time Filtering The computer operates in discreet time, therefore continuous-time has to be simulated. We will use Simulink for this purpose. Open the prepared Simulink model with the command, open_system(’LeSpContinousFiltering’). Run the simulation with the command, sim(’LeSpContinousFiltering’) or click the “play” button in the Simulink window. Hint: When opening the scopes try Autoscale from the icon bar. (a) Implement continuous time filters as you did in the previous question in the high- and lowpass filter blocks by double clicking them and changing the numerator and denominator coefficients. Hint! You can write something like: Lp.num{1} or Lp.den{1} to extract coefficients from a workspace variable called “Lp”. Select new suitable break frequencies to separate the two signal that you can see in the Scope component. 3 TMMS04 Computer Lesson – Signal Processing 2014 HT-1 (b) After simulation, the resulting data will end up in your Matlab workspace as Lp_data and Hp_data. Plot the two curves vs. the original data Org_data. Tune the corner frequencies until you feel that the two signals are separated “enough”. (c) What happens with the Hp_data and Lp_data in the very beginning and why? Hint! The filter output is based on the current input and its history. Task 4: Frequency Analysis To be able to design appropriate filters, you must know the frequencies contained within a signal. There are many ways of doing so. One way, is to generate a Power Spectral Density (PSD), using Fast Fourier Transform (FFT). According to http://www.mathworks.se/help/signal/ ug/psd-estimate-using-fft.html (2013-09-10), one way of doing so is: N = length ( x ); xdft = fft ( x ); xdft = xdft (1: N /2+1); psdx = (1/( Fs * N )).* abs ( xdft ).^2; psdx (2: end -1) = 2* psdx (2: end -1); freq = 0: Fs / length ( x ): Fs /2; plot ( freq ,10* log10 ( psdx )); grid on ; title ( ’ Periodogram Using FFT ’ ); xlabel ( ’ Frequency ( Hz ) ’ ); ylabel ( ’ Power / Frequency ( dB / Hz ) ’ ); (a) Your first task is to write a reusable Matlab script function that performs the above. The function should take as input the data you want to to analyze and the sample-frequency of this data. It should not plot the result but rather return them to your calling script or function. Hint! Test your function with a self-generated sinus function where you know what your result should be. Include this tests in your results / report. Name your m-script file the same as the function. Your function header should look something similar to this: function [ freq , psd ] = liuIDCreatePSD ( data , Fs ) % Create PowerSpectralDensity % Syntax : [ freq , psd ] = liuIDCreatePSD ( data , Fs ) % Arguments : % data = Data vector % Fs = Data sample frequency % Returns : % freq = Frequency vector % psd = Power / Frequency >> Your code goes here << (b) Now call your function on the three signals, t1, y1, t2, y2, t3, y3 and t4, y4 stored in SP.psddl. It is the same signal in each case but how does the recorded length of the data affect the resolution of the PSD estimate? For how long have the three different signals been recorded? Are the sample frequencies of the data the same? How is the sample frequency influencing the PSD estimate? Hint! Look at the corresponding time vectors. (c) What main frequencies does the signal contain? (d) How high in frequency does the spectrum estimate go? Is there anything special about this frequency? Why does it stop here? Task 5: Time-discrete Filter Design, Converting an Analog Filter As earlier stated the computer (measurement equipment) operates in a time-discrete manner. To implement software filters in a computer you must either directly design a time-discrete filter or 4 TMMS04 Computer Lesson – Signal Processing 2014 HT-1 convert an analog one. As always there are plenty of different ways to do this, in the case you will use bi-linear transform, also commonly referred to as Tustin’s method, see Equation 1. s= 2 z−1 T z+1 (1) (a) Create a first-order analog low-pass filter with cut-off frequency 100Hz, and convert it to discrete-time by using Tustin’s method. Design the filter for a sample frequency FS = 1000Hz. Hint! Have look at the function c2d. Use the bodeplot function to plot the bode diagrams for the continuous and time-discrete filters together in the same figure. This might come in handy: bop = bodeoptions ; bop . MagLowerLimMode = ’ manual ’; bop . MagLowerLim = -20; bop . FreqUnits = ’ Hz ’; bop . PhaseMatching = ’ on ’; bodeplot ( sys , bop ); (b) How far does the time-discrete filter operate (its maximum frequency)? Is there anything special about this frequency? (c) Is the filter response the same for the continuous and time-discrete versions at the corner frequency? Task 6: Time-discrete On-line Filtering When you need to use the data immediately when it arrives from the sensor, you must do on-line filtering. This is typically the case when a computer is used to control some system or process. That is, the data enters the filter, is slightly delayed (depending on phase shift) and pops-out on the other side filtered and ready to be used. (a) Plot the highly noisy data vector SP.noisy.y and the actual signal (without noise) SP.noisy.yb. First analyze the frequency spectrum and design an appropriate time-discrete filter. (b) Filter the data, using the filter function. Hint! You can extract the filter nominator and denominator from a object created by c2d like this Gd.nom{1}, Gd.den{1} where “Gd” is your filter transfer function object. Create a new frequency spectrum for the filtered data, compare the PSD plots and compare the resulting filtered data with the original noise-free SP.noisy.yb signal. Plot in the same figure with different colors. (c) What is the filter amplification at the signals main frequency component? (d) How much phase delay is introduced? What is the time-delay due to phase delay in the filtered signal? Task 7: Time-discrete Off-line Filtering If you have collected log data from a sensor and want to look at it on the computer afterwards you can do off-line filtering. This gives you an advantage over on-line filtering since you now have access to all of the data, the entire time-series. Lets reuse the data from the last question, but this time we want to do so-called zero-phase filtering, that is, we want to filter the data without introducing a phase delay. (a) Use the filtfilt command to filter the data sequence, it will run your filter once forward and then in reverse. Your filter will be applied twice. Compare the PSD after filtfilt with the one after filter was used. Compare the new filtered signal with SP.noisy.yb, and comment on the difference compared to the previous case. 5 TMMS04 Computer Lesson – Signal Processing 2014 HT-1 (b) What is the phase delay and resulting time delay? How is the amplification affected compared to the previous case? Task 8: Advanced Filter Design You have now learned how to build filters from first-order transfer-functions. You can actually create filters from any type of transfer function and this allows you to design a filter with the properties that you want. This requires deeper theoretical knowledge that you will not get from this course but luckily others have “invented” different types of more advanced filters and algorithms for designing them. (a) Have a look at the butter function. It can be used to create a Butterworth digital filter of desired order. Use the function to create five filters in rising order with the break frequency 50Hz. Design the filters for a sample frequency of your choice. Plot the bode diagrams for each filter in the same figure and comment on the differences with respect to amplification and phase delay. Hint! The filt function and the following code might come in handy. figure (811); clf ; hold on ; grid on ; bop = bodeoptions ; bop . MagLowerLimMode = ’ manual ’; bop . MagLowerLim = -100; bop . FreqUnits = ’ Hz ’; bop . PhaseMatching = ’ on ’; bodeplot ( sys , bop ); (b) Load the very noisy saw-tooth data from SP.butter.t and SP.butter.y. You want to clean up the saw-tooth signal as much as possible but you are not allowed to delay the signal more then 0.1 seconds. You also wish that the filtered signal has a remaining noise std. deviation of at most 0.075. Create a frequency spectrum, and identify the two major noise frequencies. What are they? Design a suitable Butterworth digital filter for single-pass filtering (using filter). Your design parameters are ωc and the filter order. Hint! pay attention to the help text describing ωn . You have to calculate ωn based on your desired ωc and the actual data sample frequency. What ωc , order did you choose and what ωn did you calculate? To determine the std. deviation of the remaining noise, you only need to extract and analyze the filtered data for the first ramp, (until the first drop). Hint! The following commands may be useful find, detrend, std. If you can not figure out how to use find, then you can search through the arrays manually to figure out how many elements you need to extract. What is the resulting std. deviation of the remaining noise. Also show the original and filtered data in the same plot. Use plot markers to show the maximum time delay. 3 Individual Questions The following questions are based on things you should have learned and written help functions for in the common section. They also include some selected parts from the earlier lessons. For the most part you have to figure out by yourself what you need to do to solve each question. Each student must have a personal data file. Sign up for a personal data Id at the course web page. Task 9: Good-old Sinus Wave Signal Processing In studentID.iq1 you will find a data and time vector. 6 TMMS04 Computer Lesson – Signal Processing 2014 HT-1 (a) What is the bias of the signal? What are the six different main frequencies of the signal (except for the bias)? (b) Emulate a measurement system by down-sampling the signal using the sample frequency given in studentID.iq1. Hint! Use the downsample function. You want to isolate the first main frequency component in the signal (not bias), f0 . Filter the signal before sampling (if necessary) and after sampling (if necessary) and find the actual amplitude of the f0 signal component. Motivate the design of your filters. Before, we used Simulink to simulate the continuous world, but you could also use a digital filter to emulate the anti-alias filter (if necessary). It is your choice. (c) How much amplitude and phase distortion have your filters caused on the f0 signal component? Task 10: Pressure Transducer Signal Processing and AD Conversion In studentID.iq2 you will find a data and time vector from a pressure measurement. You will also find the model number for the pressure transducer used. The data sheet for the pressure transducer can be found under Lesson 1 on the course web page. (a) The pressure signal contains N (0, a) noise (Normal distributed with zero mean and a variance. Sample the signal at 100Hz with and without anti-alias filtering and compare the results and the original data. Motivate you anti-alias filter design. (b) Now emulate the A/D-conversion (create a reusable m-script function): dData = liuID_AD(aData, adRange, nBits) The function should convert the analog input aData, to the digital output dData based on the AD-converter bit resolution given in studentID.iq2.nADbits and studentID.iq2.adRange. Hint! You may want to look at commands such as round, floor or ceil. Make sure that you test your code! It should not output values lower then zero or larger then the maximum allowed by the number of bits. Note! You do not need to convert the output value into binary form, but if you wish do so for debugging, try the commands dec2bin and bin2dec. (c) After filtering, sampling at 100Hz, and AD-conversion answer the following questions: What is the voltage resolution ∆U ? What is the pressure resolution (based on voltage measurement) ∆P ? What is the maximum pressure measured in your data by your sensor Pmax ? Task 11: Binary Data and Gyro Integration Finally! The last question. This time you must load your data from the file studentID.dat. The file contains one long data vector with gyro angular rate measurements [rad/s] at 200Hz. The data was stored to the file with the double data type and the default machine format of a Windows7 PC. (a) Load the data from the file into a local variable. As no time vector is present you must create it yourself. Plot the data vs. time. (b) Your task is to integrate the angular velocity signal to determine the absolute angle. If you do not know how to integrate a signal ask the Matlab help or the Internet. They will know! Note! The signal contains unwanted frequency components. Removed them before integration if needed. Motivate your filter design. Hint! You may want to look back to the last question from lesson 1, it may give you some ideas. Show a plot of integrated signal vs. time. What is the final angle value at the end of your integrated data? 7
© Copyright 2025 Paperzz