Defining a function in matlab

  • Thread starter Evo8
  • Start date
  • #1
169
0

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.

[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 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!
 

Answers and Replies

  • #2
169
0
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...
 
  • #3
I like Serena
Homework Helper
6,577
176
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.
 
  • #4
jhae2.718
Gold Member
1,161
20
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.
 
  • #5
169
0
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!
 
  • #6
169
0
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.....
 
  • #7
I like Serena
Homework Helper
6,577
176
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.
 
  • #8
I like Serena
Homework Helper
6,577
176
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.....
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).
 
  • #9
169
0
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."
 
  • #10
169
0
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:
  • #11
I like Serena
Homework Helper
6,577
176
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.
 
  • #12
I like Serena
Homework Helper
6,577
176
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.
 
  • #13
169
0
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!
 
  • #14
jhae2.718
Gold Member
1,161
20
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.
 
  • #15
169
0
Thank you for the clarification. I appreciate all of the help!
 
  • #16
I like Serena
Homework Helper
6,577
176
So do your plots look good now? :wink:
 
  • #17
169
0
They do indeed! Thanks again!
 

Related Threads on Defining a function in matlab

  • Last Post
Replies
5
Views
2K
Replies
1
Views
4K
Replies
1
Views
4K
  • Last Post
Replies
1
Views
2K
  • Last Post
Replies
8
Views
6K
Replies
0
Views
934
  • Last Post
Replies
1
Views
10K
  • Last Post
Replies
3
Views
2K
  • Last Post
Replies
9
Views
1K
Top