changing value in the center of an array

6 vues (au cours des 30 derniers jours)
Norman Do
Norman Do le 25 Nov 2019
Commenté : Norman Do le 29 Nov 2019
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!

Réponse acceptée

Guillaume
Guillaume le 25 Nov 2019
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 commentaires
Guillaume
Guillaume le 25 Nov 2019
Modifié(e) : Guillaume le 25 Nov 2019
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;
Norman Do
Norman Do le 29 Nov 2019
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.

Connectez-vous pour commenter.

Plus de réponses (1)

Turlough Hughes
Turlough Hughes le 25 Nov 2019
You could search for the nan value and set it equal to LPF_n0
LPF_n(isnan(LPF_n))=LPF_n0;
  3 commentaires
Turlough Hughes
Turlough Hughes le 25 Nov 2019
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;
Norman Do
Norman Do le 29 Nov 2019
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!

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by