Operands to the || and && operators must be convertible to logical scalar values.

2 visualizzazioni (ultimi 30 giorni)
I am trying to apply notch filter repeatedly to eliminate certain frequencies. It seems the for loop can't calculate the coefficients [b,a]. It says "Operands to the || and && operators must be convertible to logical scalar values." Can you please tell me what am I doing wrong? I attached the error message.
for i=locs
w0 = i/(2048/2);
bw = w0/35;
[b,a] = iirnotch(w0,bw);
dat_w = filtfilt(b,a,dat_w);
end
  1 Commento
Adam Danz
Adam Danz il 12 Ago 2020
Modificato: Adam Danz il 12 Ago 2020
Thanks for providing the full error message. That helps a lot.
It's still not clear why you're getting that error with the code you shared.
Please provide the values for locs.
One possibilitiy is that the variable and code is correct but you're not running them correctly. For example, I assume locs is a vector (presumably produced by findpeaks). When you run the full loop it should function without error. But if you run just the inside of the loop after i takes the values of locs, then i will be a vector and that will cause the exact error you're seeing.
% in r2019b
i = 1:5;
w0 = i/(2048/2);
bw = w0/35;
[b,a] = iirnotch(w0,bw);
% Operands to the || and && operators must be convertible to logical scalar values.
%
% Error in notchpeakargchk>freq_n_bandwidth (line 25)
% if (Wo<=0) || (Wo >= 1)
%
% Error in notchpeakargchk (line 12)
% msgObj = freq_n_bandwidth(Wo,BW);
%
% Error in iirnotch (line 33)
% [Ab,msgObj] = notchpeakargchk(Wo,BW,varargin);
Or perhaps locs is a column vector which would cause the same error.

Accedi per commentare.

Risposte (1)

Star Strider
Star Strider il 12 Ago 2020
Modificato: Star Strider il 12 Ago 2020
EDIT — (12 Aug 2020 at 20:30)
I suspect that ‘locs’ is a column vector, and that is causing the problems both with the loop (because it is not iterating) and with the iirnotch call because it is passing the entire ‘locs’ vector to iirnotch. That is throwing the error. The solution in that event is to transpose ‘locs’ to a row vector.
Original —
Multiple notch filters are straightforward to design in MATLAB.
I cannot tell what frequencies you want to eliminate, however this designs a FIR filter to eliminate 50 Hz mains noise and the first 3 harmonics of it (requires the Signal Processing Toolbox):
Fs = 1000;
fcomb = [[47 49.5 50.5 52], [47 49.5 50.5 52]+50, [47 49.5 50.5 52]+100, [47 49.5 50.5 52]+150];
mags = [[1 0 1], [0 1], [0 1], [0 1]];
dev = [[0.5 0.1 0.5], [0.1 0.5], [0.1 0.5], [0.1 0.5]];
[n,Wn,beta,ftype] = kaiserord(fcomb,mags,dev,Fs);
hh = fir1(n,Wn,ftype,kaiser(n+1,beta),'noscale');
figure
freqz(hh, 1, 2^20, Fs)
It would likely be relatively easy to adapt it to your problem if you provide the details.
.
  2 Commenti
Star Strider
Star Strider il 12 Ago 2020
I did the experiment (based on my experience with findpeaks, which I suspect is the origin of the ‘locs’ vector), and simulated the logical operation, returning the reported error.

Accedi per commentare.

Prodotti


Release

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by