Plotting multiple Magnitudes in one plot

13 ビュー (過去 30 日間)
Mehdi Jaiem
Mehdi Jaiem 2021 年 1 月 21 日
コメント済み: Mathieu NOE 2021 年 1 月 21 日
Hello everyone,
I intend to use freqz() function I need to plot the magnitude for different border frequencies. But first I need to determine the cut off frequencies.
I used this code but which seems to not give me even a close response to this
%% INitializing Matlab Environment
close all;
clc;
clearvars;
%% 1.1
b=log10(8000);%stop frequency and 100Hz is the pass frequency
yc1=logspace(2,b,23);
yd=stem(yc1);
grid on
xlabel('Borders','FontSize',12);
ylabel('F (Hz)','FontSize',12);
set(gca,'YScale', 'log')
%%
m={};
n={};
h={};
ph={};
j={};
fs=21e3 %sampling frequency
for i= 1:1:23
[u,o] = butter(1,[2*100/fs, (2*yc1(i))/fs],'bandpass');
m{i}=u;
n{i}=o;
%freqz(m{i},n{i});
[h{i},ph{i}]=freqz(m{i},n{i});
figure
subplot(2,1,1)
hold on
plot(ph{i},20*log10(abs(h{i})));
end
hold off
grid
set(gca,'XLim',[0 1e4],'XTick',10.^(0:4),'YLim',[-12 0],'YTick',-12:2:0,'XScale', 'log')
Any Idea on what I am missing ?

採用された回答

Star Strider
Star Strider 2021 年 1 月 21 日
Put the figure call before the loop, and remove the subplot call:
figure
hold on
for i= 1:1:23
[u,o] = butter(1,[2*100/fs, (2*yc1(i))/fs],'bandpass');
m{i}=u;
n{i}=o;
%freqz(m{i},n{i});
[h{i},ph{i}]=freqz(m{i},n{i});
plot(ph{i},20*log10(abs(h{i})));
end
hold off
This does not exactly produce the plot you posted, however it will get you closer.
I leave the other details to you.
  2 件のコメント
Mehdi Jaiem
Mehdi Jaiem 2021 年 1 月 21 日
編集済み: Mehdi Jaiem 2021 年 1 月 21 日
Thank you it solves at least the multiplot issue !
Star Strider
Star Strider 2021 年 1 月 21 日
As always, my pleasure!

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

その他の回答 (1 件)

Mathieu NOE
Mathieu NOE 2021 年 1 月 21 日
hello
according to the picture, your code needed a revision, see below
the bandwith of your filters (BW) should be proportionnal to the center frequency to have identical roll off curves when plotted in log axis
you can test with a fixed constant bandwith , it's no the right option
hope it helps !
%% INitializing Matlab Environment
close all;
clc;
clearvars;
%% 1.1
b=log10(8000);%stop frequency and 100Hz is the pass frequency
BW = 100; % fixed bandwith (Hz)
yc1=logspace(2,b,23);
yd=stem(yc1);
grid on
xlabel('Borders','FontSize',12);
ylabel('F (Hz)','FontSize',12);
set(gca,'YScale', 'log')
%%
fs=21e3 %sampling frequency
for i= 1:1:23
BW = 100; % fixed bandwith (Hz) : NO !!!
% BW should be proportionnal to center frequencies (not fixed)
BW = yc1(i)/5;
f_low = yc1(i) - BW/2;
f_high = yc1(i) + BW/2;
[B,A] = butter(1,[f_low, f_high]*2/fs,'bandpass');
freq = logspace(log10(f_low/4),log10(f_high*4),100),
[h]=freqz(B,A,freq,fs);
figure(2)
hold on
semilogx(freq,20*log10(abs(h)));
end
hold off
grid
set(gca,'XLim',[0 1e4],'XTick',10.^(0:4),'YLim',[-12 0],'YTick',-12:2:0,'XScale', 'log')
  2 件のコメント
Mehdi Jaiem
Mehdi Jaiem 2021 年 1 月 21 日
Awesome thank you!
I have to review some notons then since I am new to this topic.
But there are two ine that I did not understand:
BW = yc1(i)/5; %why did you divide by 5
freq = logspace(log10(f_low/4),log10(f_high*4),100) % what is the perpose of 100?
semilogx(freq,20*log10(abs(h))); % I read about semilogx but I did not understand it. why not plot ?
Mathieu NOE
Mathieu NOE 2021 年 1 月 21 日
BW = yc1(i)/5; %why did you divide by 5
bandwith is a fraction of center frequency , the higher the division factor, the narrower the filter ; just adapt to your own needs
freq = logspace(log10(f_low/4),log10(f_high*4),100) % what is the perpose of 100?
this is the length we want for freq vector; try another value , like 10, and see the plot being not so smooth
semilogx(freq,20*log10(abs(h))); % I read about semilogx but I did not understand it. why not plot ?
try plot to see the difference

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

カテゴリ

Help Center および File ExchangeAnalog Filters についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by