Defining Piecewise Functions in Matlab for FIR Bandpass Filter Design

In summary, the conversation discusses designing and plotting an FIR bandpass filter using MATLAB. The user is trying to define a function h(k) with a specific range for the variable k. Two different approaches are suggested, one using an array h and a range variable k, and the other using conditionals and element-wise multiplication. The user is experiencing some errors with their attempts and is seeking further help.
  • #1
Evo8
169
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 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 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
  • #2
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...
 
  • #3
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
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
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!
 
  • #6
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...
 
  • #7
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.
 
  • #8
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).
 
  • #9
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."
 
  • #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:
  • #11
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.
 
  • #12
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.
 
  • #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!
 
  • #14
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.
 
  • #15
Thank you for the clarification. I appreciate all of the help!
 
  • #16
So do your plots look good now? :wink:
 
  • #17
They do indeed! Thanks again!
 

Related to Defining Piecewise Functions in Matlab for FIR Bandpass Filter Design

What is a function in Matlab?

A function in Matlab is a set of instructions that performs a specific task. It takes in inputs, processes them, and produces outputs. Functions can be used to organize code and make it reusable.

How do you define a function in Matlab?

To define a function in Matlab, you need to use the keyword "function" followed by the name of the function and its inputs. The code for the function is then written in a separate file with the same name as the function. The function can be called from other scripts or functions in Matlab.

What is the syntax for defining a function in Matlab?

The syntax for defining a function in Matlab is:
function [output1, output2, ...] = functionName(input1, input2, ...)
% Code for the function
end

Can a function in Matlab have multiple outputs?

Yes, a function in Matlab can have multiple outputs. These outputs are specified within square brackets after the function name. The function can then return these outputs to the calling script or function.

What is the difference between a script and a function in Matlab?

A script in Matlab is a collection of code that is executed in order. It does not take in inputs or return outputs. On the other hand, a function in Matlab has inputs and outputs and can be called from other scripts or functions. Functions are also used to organize code and make it reusable.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
821
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
16
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
995
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
773
Back
Top