find function not working with double inequality

69 ビュー (過去 30 日間)
S
S 2024 年 4 月 11 日 16:00
編集済み: S 2024 年 4 月 21 日 21:38
I am trying to use a double inequality to define in my loop which outputs to display together from my filter. I am not getting any error, but my figure 2 is showing all outputs instead of just the few which fall into the designated range. To check this specific case I looked into my array and found that figure 2 should be only displaying only output 5, 6, and 7. I am redefining my output variable to include indf which is supposed to find those outputs as they fall in that range. I am unsure what is going wrong. Thank you for your time!
close all
clear all
clc
%
fs = 20e3;
numFilts = 32; %
filter_number = numFilts;
order = 4;
CenterFreqs = logspace(log10(50), log10(8000), numFilts);
% input signal definition can increase Nperiods instead of zero padding for
% resolution
signal_freq = 1000; % Hz
Nperiods = 15; % we need more than 1 period of signal to reach the steady state output (look a the IR samples)
t = linspace(0,Nperiods/signal_freq,200*Nperiods); %
input = sin(2*pi*signal_freq*t) + 0*rand(size(t));
figure%2
hold on
for ii = 1:filter_number
IR = gammatone(order, CenterFreqs(ii), fs);
[tmp,~] = freqz(IR,1,1024*2,fs);
% scale the IR amplitude so that the max modulus is 0 dB
a = 1/max(abs(tmp));
IR_array{ii} = IR*a; % scale IR and store in cell array afterwards
[h{ii},f] = freqz(IR_array{ii},1,1024*2,fs); % now store h{ii} after amplitude correction
plot(IR_array{ii})
end
title('Impulse Response');
hold off
xlim([0 1000]);
%% 3D
figure
hold on
b=50; %bandwidth
indf = find(signal_freq-b<CenterFreqs<signal_freq+b);
for ii = 1:numel(indf)
output(ii,:) = filter(IR_array{indf(ii)},1,input);
plot3(t,ii*ones(size(t)),output(ii,:))
LEGs{ii} = ['Filter # ' num2str(indf(ii))]; %assign legend name to each
end
view(-40,60); % view angles
% display integer ticks on Y axis
ax = gca;
ax.YTick = unique(round(ax.YTick));
output_sum = sum(output,1);
% plot(t,output_sum)
% LEGs{ii+1} = ['Sum'];
legend(LEGs{:})
legend('Show')
title('Filter output signals');
xlabel('Time (s)');
ylabel('Output #');
zlabel('Amplitude');
hold off

採用された回答

Voss
Voss 2024 年 4 月 11 日 16:22
編集済み: Voss 2024 年 4 月 11 日 16:31
This is not the correct way to check multiple inequalities:
indf = find(signal_freq-b<CenterFreqs<signal_freq+b);
Instead do:
indf = find( signal_freq-b<CenterFreqs & CenterFreqs<signal_freq+b );
or:
indf = find( abs(CenterFreqs-signal_freq) < b )
  10 件のコメント
Voss
Voss 2024 年 4 月 18 日 16:33
indf = find(abs(CenterFreqs-(signal_freq-s)) < b );
finds elements of CenterFreqs that are within b (=50) of signal_freq-s (=850), so within the range 800 to 900, exclusive.
indf = find( abs(CenterFreqs-(signal_freq+s)) < b );
does the same within the range 1100 to 1200, exclusive.
So what you said is right, except it's 100 (which is 2*b) not 150 (which is s).
S
S 2024 年 4 月 21 日 21:38
@Voss oh makes sense! So the first line is for lower freqs, while the second is for higher freqs. Thank you!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSpectral Measurements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by