Frontmatter

If you are publishing this notebook on the web, you can set the parameters below to provide HTML metadata. This is useful for search engines and social media.

using PlutoUI
140 ms

Filter design

Consider a sinusoid constructed from summing a 30 Hz cosine and 60 Hz sine wave.

$$x(t) = \cos(2\pi30t) + \sin(2\pi60t)$$

We are going to filter this signal using a 60 Hz notch filter to eliminate the higher frequency content.

We will do this three different ways:

  1. Using transfer function models

  2. In Simulink

  3. Using the filter design app

md"""### Filter design

Consider a sinusoid constructed from summing a 30 Hz cosine and 60 Hz sine wave.

```math
x(t) = \cos(2\pi30t) + \sin(2\pi60t)
```

We are going to filter this signal using a 60 Hz notch filter to eliminate the higher frequency content.

We will do this three different ways:

1. Using transfer function models
2. In Simulink
3. Using the filter design app

"""
7.3 ms

Transfer function approach

Recall the transfer function given in Exam 2.

$$G(s) = \dfrac{s^2+3600}{s^2+120s+3600}$$

Follow these steps in MATLAB

  1. Construct $x(t)$ above in MATLAB using a time vector of frequency 1 kHz and running from 0 to 0.5 seconds.

  2. Create the above transfer function in MATLAB using the tf command.

  3. Convert this continuous time model to a discrete time model using the c2d command; be sure to specify the correct sampling time Ts. The transfer function is for rad/s while in discrete time MATLAB uses Hz.

  4. Extract the numerator and denominator coefficients from the converted model and pass those to the filtfilt command to filter x. Call this output y.

  5. Plot both x and y on the same graph.

md"""## Transfer function approach

Recall the transfer function given in Exam 2.

```math
G(s) = \dfrac{s^2+3600}{s^2+120s+3600}
```
Follow these steps in MATLAB

1. Construct $x(t)$ above in MATLAB using a time vector of frequency 1 kHz and running from 0 to 0.5 seconds.
1. Create the above transfer function in MATLAB using the [`tf`](https://www.mathworks.com/help/control/ref/tf.html) command.
2. Convert this continuous time model to a discrete time model using the [`c2d`](https://www.mathworks.com/help/ident/ref/dynamicsystem.c2d.html) command; be sure to specify the _correct_ sampling time `Ts`. The transfer function is for rad/s while in discrete time MATLAB uses Hz.
3. Extract the numerator and denominator coefficients from the converted model and pass those to the [`filtfilt`](https://www.mathworks.com/help/signal/ref/filtfilt.html) command to filter `x`. Call this output `y`.
4. Plot both `x` and `y` on the same graph.

9.5 ms

Here is some starter code:

t = ___;
x = ___;

gain = 1;
p7num = gain* ___;
p7den = ___;
p7tf = tf(p7num, p7den);
dt_tf = c2d(p7tf, ___);
y = filtfilt(dt_tf.Numerator{:}, dt_tf.Denominator{:}, ___);
.
.
.
md""" Here is some starter code:

```matlab
t = ___;
x = ___;

gain = 1;
p7num = gain* ___;
p7den = ___;
p7tf = tf(p7num, p7den);
dt_tf = c2d(p7tf, ___);
y = filtfilt(dt_tf.Numerator{:}, dt_tf.Denominator{:}, ___);
.
.
.
```


"""
141 μs

Questions:

  • How can we get the output to be similar in magnitude to the input?

  • Does the output seem time shifted? How could we take care of this?

    • Look up the grpdelay command. It might be useful for your project.

md"""
Questions:

* How can we get the output to be similar in magnitude to the input?
* Does the output seem time shifted? How could we take care of this?
- Look up the [`grpdelay`](https://www.mathworks.com/help/signal/ref/grpdelay.html) command. It might be useful for your project.
"""
1.1 ms

Simulink approach

Perform the same exercise in Simulink. Here is a starter diagram:

md"""## Simulink approach

Perform the same exercise in Simulink. Here is a starter diagram:
"""
549 μs
Resource("https://i.ibb.co/GvkKhXM/Screenshot-2023-04-16-at-10-53-15-AM.png")
3.1 ms

Remember ...

  1. Phase shift the sine block to get a cosine wave.

  2. Sine block is in rad/s

  3. The square box above is a gain slider. You can use a standard gain.

  4. You would need to modify the scope block to increase number of accepted inputs; but you probbaly want to output the variable to MATLAB workspace for making subplots.

md""" Remember ...

1. Phase shift the sine block to get a cosine wave.
2. Sine block is in rad/s
3. The square box above is a gain slider. You can use a standard gain.
4. You would need to modify the scope block to increase number of accepted inputs; but you probbaly want to output the variable to MATLAB workspace for making subplots.
"""
1.6 ms

Use the filter design app

This section requires the use of the Signal Processing Toolbox. To start the filter designer tool run filterDesigner command from the MATLAB command window.

>> filterDesigner

Then, create a filter with the following specifications:

md"""## Use the filter design app

This section requires the use of the Signal Processing Toolbox. To start the filter designer tool run `filterDesigner` command from the MATLAB command window.

```>> filterDesigner```

Then, create a filter with the following specifications:
"""
533 μs
Resource("https://i.ibb.co/k6kCr67/Screenshot-2023-04-16-at-11-20-15-AM.png")
47.4 μs

Next

  1. From the File menu export this filter as a MATLAB function (give an appropriate name of your choosing). Save it as a .m file.

  2. Call this function to create a 1x1 df2 filter object in your MATLAB workspace.

  3. Use the created filter object from your Workspace to filter your x variable and save the result to y. For this use our old friend the filter command (see: the Tips section).

  4. Plot both of them on the same figure as before.

Compare the outputs of the first method and this one. What is different?

md"""
Next

1. From the `File` menu export this filter as a MATLAB function (give an appropriate name of your choosing). Save it as a `.m` file.
2. Call this function to create a `1x1 df2` filter object in your MATLAB workspace.
3. Use the created filter object from your Workspace to filter your `x` variable and save the result to `y`. For this use our old friend the `filter` command (see: the [Tips section](https://www.mathworks.com/help/matlab/ref/filter.html#bt_7huw-5)).
4. Plot both of them on the same figure as before.


Compare the outputs of the first method and this one. What is different?
"""
1.0 ms

To turn in ...

Generate a single figure with three subplots consisting of figures from each of the three methods above. For example

subplot(1, 3, 1)
.
.
.
title("Output using TF method")
.
.
subplot(1, 3, 3)
.
.
title("Output from filter designer")

Answer questions above.

md"""## To turn in ...

Generate a single figure with three subplots consisting of figures from each of the three methods above. For example

```
subplot(1, 3, 1)
.
.
.
title("Output using TF method")
.
.
subplot(1, 3, 3)
.
.
title("Output from filter designer")
```
Answer questions above.
"""
449 μs