# Defining a function in matlab

## Homework Statement

Ok so Im slowly learning the syntax and various functions of Matlab. I am triyng to design and plot an FIR bandpass filter. Below is a coefficent 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.

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

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 dont understand is how to put these ranges for the variable k within each string? Above I am defining it for the entire simulation. I dont even know how to properly search for this in google as I dont know what it would be called. I cant image it is too complicated but I just dont know the syntax. Ive tried a couple of different ways but nothing really worked correctly.

Any help would be much appreciated!

Related Engineering and Comp Sci Homework Help News on Phys.org
Now looking at what ive 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. Im trying to google it but i dont know what to search for...

I like Serena
Homework Helper
Hi Evo8! 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.

jhae2.718
Gold Member
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 wont like it because the range of k is defined in two different areas. Maybe there is a hierarchy in matlab that i dont 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 dont see why you have the first one in there?

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

I also cant 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 im trying to understand whats going on in the code.

I appreciate the help!

In addition Matlab doesnt 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.....

I like Serena
Homework Helper
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 wont like it because the range of k is defined in two different areas. Maybe there is a hierarchy in matlab that i dont 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.

I like Serena
Homework Helper
In addition Matlab doesnt 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.....
You're assigning the variable "o" (small letter 'o'), but that variable is not defined.
I intended the integer zero (0).

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:
I like Serena
Homework Helper
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.

I like Serena
Homework Helper
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!

jhae2.718
Gold Member
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 dont 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.

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.

I also cant seem to find any information on the double equals sign.
$$==$$
As it's been said, == is a comparison and = is for assignment.

Last question is why the +0 at the end? Sorry for all of the questions but im trying to understand whats 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!

I like Serena
Homework Helper
So do your plots look good now? They do indeed! Thanks again!