フィルターのクリア

how can i use legend command when i am using hold on for plotting graphs? in my plot i want to use legend command to indicate the color for respective plot that is black for low pass,blue for high pass,green for power complementary,red for all pass.

10 ビュー (過去 30 日間)
k = input('Number of frequency points = '); %%256
w = 0:pi/k:pi;
alpha=0.3
num=((1-alpha)/2)*[1 1]
den=[1 -alpha]
hlp=freqz(num,den,w)
num1=((1+alpha)/2)*[1 -1]
den1=[1 -alpha]
hhp=freqz(num1,den1,w)
figure
subplot(2,1,1)
plot(w/pi, abs(hlp),'k','LineWidth', 2);hold on
grid on;
xlabel('\omega/\pi', 'FontSize', 12, 'FontWeight', 'Bold')
ylabel('Amplitude', 'FontSize', 12, 'FontWeight', 'Bold')
title('|H(\omega)|', 'FontSize', 12, 'FontWeight', 'Bold')
set(gca, 'FontSize', 14, 'FontWeight', 'Bold')
legend ('low pass','high pass','all pass','Location', 'Northwest')
set(legend,'FontSize',7);
subplot(2,1,2)
plot(w/pi, angle(hlp),'k', 'LineWidth', 2);hold on;
grid on;
xlabel('\omega/\pi', 'FontSize', 12, 'FontWeight', 'Bold')
ylabel('Phase, radians', 'FontSize', 12, 'FontWeight', 'Bold')
title('\angle H(\omega)', 'FontSize', 12, 'FontWeight', 'Bold')
set(gca, 'FontSize', 14, 'FontWeight', 'Bold')
% legend (ax2,'hlp','hhp','all_pass','Location', 'Northwest')
% set(legend,'FontSize',7);
subplot(2,1,1)
plot(w/pi, abs(hhp), 'LineWidth', 2);hold on
grid on;
xlabel('\omega/\pi', 'FontSize', 12, 'FontWeight', 'Bold')
ylabel('Amplitude', 'FontSize', 12, 'FontWeight', 'Bold')
title('|H(\omega)|', 'FontSize', 12, 'FontWeight', 'Bold')
set(gca, 'FontSize', 14, 'FontWeight', 'Bold')
subplot(2,1,2)
plot(w/pi, angle(hhp), 'LineWidth', 2);hold on;
grid on;
xlabel('\omega/\pi', 'FontSize', 12, 'FontWeight', 'Bold')
ylabel('Phase, radians', 'FontSize', 12, 'FontWeight', 'Bold')
title('\angle H(\omega)', 'FontSize', 12, 'FontWeight', 'Bold')
set(gca, 'FontSize', 14, 'FontWeight', 'Bold')
all_pass=abs(hlp+hhp)
%figure
subplot(2,1,1)
plot(w/pi, abs(all_pass),'r', 'LineWidth', 2);hold on
grid on;
xlabel('\omega/\pi', 'FontSize', 12, 'FontWeight', 'Bold')
ylabel('Amplitude', 'FontSize', 12, 'FontWeight', 'Bold')
title('|H(\omega)|', 'FontSize', 12, 'FontWeight', 'Bold')
set(gca, 'FontSize', 14, 'FontWeight', 'Bold')
subplot(2,1,2)
plot(w/pi, angle(all_pass),'r', 'LineWidth', 2);hold on;
grid on;
xlabel('\omega/\pi', 'FontSize', 12, 'FontWeight', 'Bold')
ylabel('Phase, radians', 'FontSize', 12, 'FontWeight', 'Bold')
title('\angle H(\omega)', 'FontSize', 12, 'FontWeight', 'Bold')
set(gca, 'FontSize', 14, 'FontWeight', 'Bold')
%%power complentary
powr=(abs(hhp).^2)+(abs(hlp).^2)
figure
subplot(2,1,1)
plot(w/pi, abs(hlp),'k', 'LineWidth', 2);hold on;
grid on;
xlabel('\omega/\pi', 'FontSize', 12, 'FontWeight', 'Bold')
ylabel('Amplitude', 'FontSize', 12, 'FontWeight', 'Bold')
title('|H(\omega)|','FontSize', 12, 'FontWeight', 'Bold')
set(gca, 'FontSize', 14, 'FontWeight', 'Bold')
legend ('hlp','hhp','powr','Location', 'Northwest')
set(legend,'FontSize',7);
subplot(2,1,2)
plot(w/pi, angle(hlp),'k', 'LineWidth', 2);hold on;
grid on;
xlabel('\omega/\pi', 'FontSize', 12, 'FontWeight', 'Bold')
ylabel('Phase, radians', 'FontSize', 12, 'FontWeight', 'Bold')
title('\angle H(\omega)', 'FontSize', 12, 'FontWeight', 'Bold')
set(gca, 'FontSize', 14, 'FontWeight', 'Bold')
legend ('hlp','hhp','powr','Location', 'Northwest')
set(legend,'FontSize',7);
subplot(2,1,1)
plot(w/pi, abs(hhp), 'LineWidth', 2);hold on
grid on;
xlabel('\omega/\pi', 'FontSize', 12, 'FontWeight', 'Bold')
ylabel('Amplitude', 'FontSize', 12, 'FontWeight', 'Bold')
title('|H(\omega)|', 'FontSize', 12, 'FontWeight', 'Bold')
set(gca, 'FontSize', 14, 'FontWeight', 'Bold')
subplot(2,1,2)
plot(w/pi, angle(hhp), 'LineWidth', 2);hold on;
grid on;
xlabel('\omega/\pi', 'FontSize', 12, 'FontWeight', 'Bold')
ylabel('Phase, radians', 'FontSize', 12, 'FontWeight', 'Bold')
title('\angle H(\omega)', 'FontSize', 12, 'FontWeight', 'Bold')
set(gca, 'FontSize', 14, 'FontWeight', 'Bold')
subplot(2,1,1)
plot(w/pi, abs(powr),'g', 'LineWidth', 2);hold on
grid on;
xlabel('\omega/\pi', 'FontSize', 12, 'FontWeight', 'Bold')
ylabel('Amplitude', 'FontSize', 12, 'FontWeight', 'Bold')
title('|H(\omega)|', 'FontSize', 12, 'FontWeight', 'Bold')
set(gca, 'FontSize', 14, 'FontWeight', 'Bold')
subplot(2,1,2)
plot(w/pi, angle(powr),'g', 'LineWidth', 2);hold on;
grid on;
xlabel('\omega/\pi', 'FontSize', 12, 'FontWeight', 'Bold')
ylabel('Phase, radians', 'FontSize', 12, 'FontWeight', 'Bold')
title('\angle H(\omega)', 'FontSize', 12, 'FontWeight', 'Bold')
set(gca, 'FontSize', 14, 'FontWeight', 'Bold')

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2015 年 6 月 20 日
編集済み: Azzi Abdelmalek 2015 年 6 月 20 日
Use the legend function after the three plots
close all
k = input('Number of frequency points = '); %%256
w = 0:pi/k:pi;
alpha=0.3
num=((1-alpha)/2)*[1 1]
den=[1 -alpha]
hlp=freqz(num,den,w)
num1=((1+alpha)/2)*[1 -1]
den1=[1 -alpha]
hhp=freqz(num1,den1,w)
figure
subplot(2,1,1)
plot(w/pi, abs(hlp),'k','LineWidth', 2);hold on
grid on;
xlabel('\omega/\pi', 'FontSize', 12, 'FontWeight', 'Bold')
ylabel('Amplitude', 'FontSize', 12, 'FontWeight', 'Bold')
title('|H(\omega)|', 'FontSize', 12, 'FontWeight', 'Bold')
set(gca, 'FontSize', 14, 'FontWeight', 'Bold')
plot(w/pi, abs(hhp), 'LineWidth', 2);hold on
grid on;
xlabel('\omega/\pi', 'FontSize', 12, 'FontWeight', 'Bold')
ylabel('Amplitude', 'FontSize', 12, 'FontWeight', 'Bold')
title('|H(\omega)|', 'FontSize', 12, 'FontWeight', 'Bold')
set(gca, 'FontSize', 14, 'FontWeight', 'Bold')
plot(w/pi, abs(all_pass),'r', 'LineWidth', 2);hold on
grid on;
xlabel('\omega/\pi', 'FontSize', 12, 'FontWeight', 'Bold')
ylabel('Amplitude', 'FontSize', 12, 'FontWeight', 'Bold')
title('|H(\omega)|', 'FontSize', 12, 'FontWeight', 'Bold')
set(gca, 'FontSize', 14, 'FontWeight', 'Bold')
legend ('low pass','high pass','all pass','Location', 'Northwest')
set(legend,'FontSize',7);
  2 件のコメント
Mebin Cherian
Mebin Cherian 2020 年 11 月 25 日
But what is we are plotting results from an iteration, and the number f iteration is specified during execution of the code?

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGraphics Object Properties についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by