1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Defining a function in matlab

  1. Jan 25, 2012 #1
    1. The problem statement, all variables and given/known data

    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

    2. Relevant equations

    N/A

    3. 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!
     
  2. jcsd
  3. Jan 26, 2012 #2
    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...
     
  4. Jan 26, 2012 #3

    I like Serena

    User Avatar
    Homework Helper

    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.
     
  5. Jan 26, 2012 #4

    jhae2.718

    User Avatar
    Gold Member

    You can break the function up manually (like ILSe suggested), use if statements, or use conditionals like this:
    Code (Text):

    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.
     
  6. Feb 1, 2012 #5
    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!
     
  7. Feb 1, 2012 #6
    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.....
     
  8. Feb 1, 2012 #7

    I like Serena

    User Avatar
    Homework Helper

    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.
     
  9. Feb 1, 2012 #8

    I like Serena

    User Avatar
    Homework Helper

    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).
     
  10. Feb 1, 2012 #9
    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."
     
  11. Feb 1, 2012 #10
    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: Feb 1, 2012
  12. Feb 1, 2012 #11

    I like Serena

    User Avatar
    Homework Helper

    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.
     
  13. Feb 1, 2012 #12

    I like Serena

    User Avatar
    Homework Helper

    "=" 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.
     
  14. Feb 1, 2012 #13
    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!
     
  15. Feb 1, 2012 #14

    jhae2.718

    User Avatar
    Gold Member

    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.

    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.

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

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

    I like Serena

    User Avatar
    Homework Helper

    So do your plots look good now? :wink:
     
  18. Feb 5, 2012 #17
    They do indeed! Thanks again!
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Defining a function in matlab
  1. MATLAB Function (Replies: 0)

Loading...