changing value in the center of an array

Hi, I would like to change the value of the center of any array:
This is my example code:
-I am designing an LPF filter
-I resolved the LPF filter, in correlation to n (LPF(n) = sin(omegac*n)./(n*pi)).
-HOWEVER, my center value is NaN (not applicable anyways).
-I'd like to change the center value of the array LPF_n with LPF_n0.
-For example: LPF_n = [1, 3, 5, NaN, 7, 8, 12]
-LPF_n0 = 3
-I want LPF_n = [1, 3, 5, "3", 7, 8, 12] (I highlighted "3" for better understanding, it doesn't belong there)
-My vectors will change, according to n values. So I attempted to do it by trying this:
fs = 8000
fc = 900
tap = 31
M = (taps-1) / 2
n = [ -M:M ]
omegac = (2*pi*fc)/fs
LPF_n = sin(omegac*n)./(n*pi)
LPF_n0 = omegac/pi
x = length(LPF_n)
x_2 = [x-1/2,0]
LPF_n(x_2) = LPF_n0
Any help is greatly appreciated, Thank you!

 Accepted Answer

What's an LPF filter? Possibly, is it a Low Pass Filter filter?
I don't understand what you're trying to achieve with this
x_2 = [x-1/2,0]
which creates a vector of two numbers, the second one is 0, the first one is the length of your filter minus half. Why do you think it can be useful as an index and why do you think it would refer to the middle of the array.
Anyway, assuming the length of your vector is odd:
LPF_n((end+1)/2) = LPF_n0;
will put LPF_n0 in the middle.
If the length is even, then you need to round (end+1)/2 up or down depending on the edge of which half you consider to be the middle.

3 Comments

Norman Do
Norman Do on 25 Nov 2019
Edited: Norman Do on 25 Nov 2019
LPF is an LPF filter, I'm working on designing a "specified" filter in DSP class. This is applied across LPF, HPF, BP(ass), and BS(top) filters.
So my elementary attempt at find the center value, was to find the length of the LPF_n, then I take that and make it "x".
for x_2, I took took that length, subtracted 1, and divided by 2, to find the center value of the array.
The reason for me having LPF_n and LPF_n0, is because at 0, the LPF_n equation doesn't solve the values at 0, therefore I have an LPF_n0.
MANY MANY thanks! You and the person above resolved it in both ways that I've wanted.
Oh, so your x-1/2 was an attempt at (x-1)/2, two completely different expressions which won't give the same result at all, the former is x-0.5. In any case, to get the centre index, it's (x+1)/2.
Note that the end keyword is a shorter way of writing length(x) (for vectors, for matrices it's a shortcut to the size of the dimension in which end is used).
If you want to replace the first element, which is index 1 not 0, at the same time as the middle one, then:
LPF_n([1, (end+1)/2]) = LPF_n0;
grrr.... You're right, my grammatical errors are very strong when I do coding calculations. Thank you for the clarification!
I will keep the "end" function in mind for next time, many thanks!
FWIW, I am still quite new to MATLab. You think you've learned most of it all, but it's always still a learning process for me.

Sign in to comment.

More Answers (1)

You could search for the nan value and set it equal to LPF_n0
LPF_n(isnan(LPF_n))=LPF_n0;

3 Comments

Norman Do
Norman Do on 25 Nov 2019
Edited: Norman Do on 25 Nov 2019
This does it!
TYVM!!
Just for curiosity..... How would one be able to change JUST the center value of the vector?
This resolves this issue, but I'd like to know (for my knowledge), how one would pick a center value in an array of any size?
EDIT: Nevermind, the person below also resolved it. Thank you!
Happy to help. You get NaNs at n=0 because that results in 0/0 in your equation for LPF. Although since there are other ways you can end up with NaNs it would be better to find where n=0 rather than using isnan and then letting LPF=LPF_n0 at the corresponding indices:
LPF(n==0)=LPF_n0;
Ahh, that makes sense, thank you so much! I will keep this in mind for later also.
Once again many thanks!
I wish I could accept both of your answers as the accepted answer!

Sign in to comment.

Asked:

on 25 Nov 2019

Commented:

on 29 Nov 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!