i have this code which adds the 3rd harmonic to the fundamental signal and then using a filter takes back the fundamental. I have to modify this code in order to add the 3rd, 5th and 7th harmonic and then filter them out, but i dont know how to make the filter do that.

```
t = [0:199];
A1 = 100;
s1 = A1*sin(2*pi*(1/40)*t); % fundamental
A3 = A1/3;
s3 = A3*sin(2*pi*(1/40)*3*t); %3rd harmonic
s13 = s1 + s3;
% A5 = A1/5;
% s5 = A5*sin(2*pi*(1/40)*5*t); 5th
% A7 = A1/7;
% s7 = A7*sin(2*pi*(1/40)*7*t); 7th
% filter
[b13, a13] = ellip(6,0.5,20,[5.7/40 6.3/40],'stop') %elliptic filter
h13 = impz(b13,a13,length(s13)); %impulse
y13 = filter(b13,a13,s13);
```

# Best How To :

The easiest way of getting rid of those harmonics is to simply to a low-pass filter...which will get rid of ALL frequency content above your cutoff. This is no longer a notch filter, like you showed, but it will certainly get rid of tall of those harmonics:

```
%% lowpass IIR filter example
fs_Hz = 1; %your sample rate appears to be 1 Hz
fund_Hz = 1/40; %this is your fundamental
cutoff_Hz = 1.5*fund_Hz; %choose cutoff
[b,a] = butter(3,fund_Hz/(fs_Hz/2)); %lowpass by default
y = filter(b,a,s13); %apply filter
```

If a lowpass filter is too much filtering, then it sounds like your problem is that you don't know how to do a multi-notch filter. That's fine. You can choose to notch our your harmonics by applying a series of notch filters one-after-the-other...

```
%% apply IIR notch filters in series
fs_Hz = 1; %your sample rate
fund_Hz =1/40; %your fundamental frequency
y = x; %initialize your output
for Iharm = 3:2:7 %will do 3rd, 5th, 7th
[b, a] = ellip(6,0.5,20,[(Iharm-0.3) (Iharm+0.3)]*fund_Hz/(fs_hz/2),'stop');
y = filter(b,a,y); %apply the filter onto the previous output
end
```

And, finally, if you want to do it all as one filter, you'll need a much more complicated filter. You could design your own based on poles and zeros (which is probably what is intended, if this is a class project, which is what it sounds like). Or, you could use one of the filter design commands that allows you to enter an arbitrary response. If you want to use IIR filters (as opposed to FIR filters), look up the `yulewalk`

command. You'd use it something like this (I haven't used it in a while, so this might not be right...

```
%% yulewalk example
f_Hz = 1; %your sample rate
f_fund_Hz = 1/40; %your fundamental
%define desired response at different frequencies
w = 0.3; %width of each notch
f_Hz = [3-w 3 3+w 5-w 5 5+w 7-w 7 7+w]*f_fund_Hz; %3rd, 5th, 7th harmonics
resp = [ 1 0 1 1 0 1 1 0 1 ]; %notch each harmonic
f_Hz = [0; f_Hz(:); fs/2]; %must start at 0 Hz and end at Nyquist
resp = [1; resp(:); 1]; %add start and end points
%create and apply the filter
N = 3*3; %filter order...play with this...N=3 per notch?
[b,a]=yulewalk(N,f_Hz/(fs_Hz/2),resp); %create filter
y = filter(b,a,x); %apply filter
```