FIR bandpass filter coefficients with piecewise definition in Matlab

  • Thread starter Thread starter Evo8
  • Start date Start date
  • Tags Tags
    Function Matlab
Join the discussion
Registration is free. Start your own thread to ask a follow-up.
16 replies · 4K views
Evo8
Messages
168
Reaction score
0

Homework Statement



Ok so I am slowly learning the syntax and various functions of Matlab. I am triyng to design and plot an FIR bandpass filter. Below is a coefficient h(k). I need to define this h(k) in 3 parts from what I understand. for k=0:p-1, k=p, and k=p+1:m to satisfy.

[itex]h(k)= \frac{sin(2 \pi(k-p)F_{1}T)-sin(2 \pi(k-p)F_{0}T)}{\pi(k-p)}[/itex]for o≤k≤m,k≠0
[itex]h(k)=2(F_{1}-F_{0})T[/itex] for k=p
[itex]h(k)=0 [/itex] otherwise

Homework Equations



N/A

The Attempt at a Solution


This is what I have
k=0:m;
h1=(sin(2*pi*(k-p)*f1)-sin(2*pi*(k-p)*f0))/(pi*(k-p));


This not fully correct. What I don't understand is how to put these ranges for the variable k within each string? Above I am defining it for the entire simulation. I don't even know how to properly search for this in google as I don't know what it would be called. I can't image it is too complicated but I just don't know the syntax. I've tried a couple of different ways but nothing really worked correctly.

Any help would be much appreciated!
 
Physics news on Phys.org
Now looking at what I've written maybe I wouldn't call it defining a function in MATLAB since this produces something different then I need.

I just need to defin this h(k) at certain ranges. I would call h(k) a function this is why I used the word.

Im still stuck on this. I am trying to google it but i don't know what to search for...
 
Hi Evo8! :smile:

I do not know or have Matlab, but as a software engineer I can hazard a few educated guesses.

Your attempt probably won't work, since your expression is not defined for k=p, which is included in the range 0:m.

I'd try something like this:
h(0)=0;
k=1:p-1;
h(k)=(sin(2*pi*(k-p)*f1)-sin(2*pi*(k-p)*f0))/(pi*(k-p));
h(p)=2(f1−f0)T;
k=p+1:m;
h(k)=(sin(2*pi*(k-p)*f1)-sin(2*pi*(k-p)*f0))/(pi*(k-p));

In my suggestion k is repeatedly used as a range variable and h is an array that is indexed using a range variable.
 
You can break the function up manually (like ILSe suggested), use if statements, or use conditionals like this:
Code:
k = 0:m;
h = ((0<k) && (k<=(p-1)) .* (sin(2*pi*(k-p).*f1)-sin(2*pi*(k-p).*f0.*T))./(pi*(k-p)) + ...
    (k==p) * 2 *(f1-f0).*T + 0;
MATLAB will evaluate the conditions as 1 or 0, so if the conditional holds the term remains and if not it's multiplied by zero.
 
IlSe

Thanks for your response. I understand how you have everything written there and this may work. I will give something like this a try however i feel Matlab won't like it because the range of k is defined in two different areas. Maybe there is a hierarchy in MATLAB that i don't really understand. I will have to play with this. h(k) is also defined twice so it may not like this.

jhae2.718, Thanks for your response as well. I am a little confused by some of the operators and syntax used in what youve posted below.

First could you explain the use of? $$.*$$

From looking this up I see that it is the element wise multiplication of arrays. I don't see why you have the first one in there?

Also the
$$&&$$
These are like an and operator right? Why two of them?

I also can't seem to find any information on the double equals sign.
$$==$$

Last question is why the +0 at the end? Sorry for all of the questions but I am trying to understand what's going on in the code.

I appreciate the help!
 
In addition Matlab doesn't seem to like the expression
$$h(0)=o;$$

I get an error saying that the index must be a positive integer or logical. I see that this expression was supposed to satisfy my 0 otherwise statement...
 
Evo8 said:
IlSe

Thanks for your response. I understand how you have everything written there and this may work. I will give something like this a try however i feel Matlab won't like it because the range of k is defined in two different areas. Maybe there is a hierarchy in MATLAB that i don't really understand. I will have to play with this. h(k) is also defined twice so it may not like this.

I expect that it will work, but please try it.

k is a variable that can be assigned different values.
If you assign it a different range (a range counts as a value), the value of k changes to this new range that has effect from then onward.

h is an array that contains multiple values.
You can access a single value in that array with for instance h(1).
Or you can access a subset of values in the array with for instance h(1:3).
In both cases the part of the array that is not accessed will remain unchanged.
 
Evo8 said:
In addition Matlab doesn't seem to like the expression
$$h(0)=o;$$

I get an error saying that the index must be a positive integer or logical. I see that this expression was supposed to satisfy my 0 otherwise statement...

Try h(0)=0 instead.
You're assigning the variable "o" (small letter 'o'), but that variable is not defined.
I intended the integer zero (0).
 
I like Serena said:
Try h(0)=0 instead.
You're assigning the variable "o" (small letter 'o'), but that variable is not defined.
I intended the integer zero (0).

opps! That was a typo here in the form but not in matlab. Sorry about that I did infact use h(0)=0 in my code. I just tried again and I get the same error. "Subscript indices must either be real positive integers or logicals."
 
Should it be something like;
k=0;
h(k)=0;

This is an extra line of code but the same thing essentially.

Edit: nevermind i just tried it and it does the same thing.

I also found out what the == means. Its a logical if statement. Let me play with the code a little more and maybe i can figure it out. ill report back
 
Last edited:
Evo8 said:
opps! That was a typo here in the form but not in matlab. Sorry about that I did infact use h(0)=0 in my code. I just tried again and I get the same error. "Subscript indices must either be real positive integers or logicals."

Okay, the error means that the array h can not be indexed with position 0.
Apparently MATLAB has arrays that start at the index 1.
So just loose the h(0)=0 statement and you're done.
Index position 0 is not relevant anyway.
 
Evo8 said:
I also found out what the == means. Its a logical if statement. Let me play with the code a little more and maybe i can figure it out. ill report back

"=" would mean assignment, where you assign an expression to a variable.
"==" means a check on equality, where you compare 2 expressions and check whether they are equal or not.
 
Ok this seems to work. I see what you mean with indexing at 0.

I omitted this line and it seems to work. Now I just need to figure out why my plots looks weird. Forming this array was just part of the lab. A little more finagling and well see what I get.

Thanks again for all of the help!
 
Evo8 said:
jhae2.718, Thanks for your response as well. I am a little confused by some of the operators and syntax used in what youve posted below.

First could you explain the use of? $$.*$$

From looking this up I see that it is the element wise multiplication of arrays. I don't see why you have the first one in there?
The dot does indeed indicate element wise operations. As for the first one, I have a habit of being overly explicit when doing element wise operations.

Evo8 said:
Also the
$$&&$$
These are like an and operator right? Why two of them?
In MATLAB, & and && are both symbols used for a logical AND. Using && results in MATLAB not bothering to check the second condition if the first is false.

Evo8 said:
I also can't seem to find any information on the double equals sign.
$$==$$

As it's been said, == is a comparison and = is for assignment.

Evo8 said:
Last question is why the +0 at the end? Sorry for all of the questions but I am trying to understand what's going on in the code.

The + 0 isn't necessary, but just explicitly shows that the value is zero if the other conditions don't apply.
 
Thank you for the clarification. I appreciate all of the help!