MATLAB Answers

Why are the legends appearing twice?

3 views (last 30 days)
Giggs B.
Giggs B. on 15 May 2021
Commented: Giggs B. on 16 May 2021
Hi,
Please refer to the image. I don't know why my legends are appearing twice...can someone please help me? I tried a couple of things, but couldn't fix. I have been struggling with this for 3 days now. Thank you. (I am using MATLAB 2018).
close all;
%1
[y,fs]=audioread('crowd.mp3');
y_h=highpass(y,200,fs);
N=length(y);
time=N/fs;
t = linspace(0, time, N);
referenceLength=length(y);
fref=fs*(1:referenceLength)/referenceLength;
R=fft(y_h,referenceLength);
stor=R(1);
R(1)=0;
absR=abs(R);
plot(fref,absR,'DisplayName','crowd','color','b');
xlim([200 9000]);
ylim([0 200]);
xticks([200 1000 2000 3000 4000 5000 6000 7000 8000 9000]);
yticks([0 25 50 75 100 125 150 175 200]);
grid on;
xlabel('Frequency(Hz)');
ylabel('Amplitude');
title('Spectrum plot of different noises');
hold on;
%2
[y1,fs]=audioread('cafeteria.mp3');
y_h1=highpass(y1,200,fs);
fref=fs*(1:referenceLength)/referenceLength;
R1=fft(y_h1,referenceLength);
stor1=R1(1);
R1(1)=0;
absR1=abs(R1);
figure(1);
plot(fref,absR1,'DisplayName','Cafeteria','color','r');
hold on;
%3
[y2,fs]=audioread('shower.mp3');
y_h2=highpass(y2,200,fs);
fref=fs*(1:referenceLength)/referenceLength;
R2=fft(y_h2,referenceLength);
stor2=R2(1);
R2(1)=0;
absR2=abs(R2);
plot(fref,absR2,'DisplayName','Bathroom','color','g');
hold on;
%4 )
[y3,fs]=audioread('desk.mp3');
y_h3=highpass(y3,200,fs);
fref=fs*(1:referenceLength)/referenceLength;
R3=fft(y_h3,referenceLength);
stor3=R3(1);
R3(1)=0;
absR3=abs(R3);
plot(fref,absR3,'DisplayName','desk','color','y');
hold on;
%5
[y4,fs]=audioread('office.mp3');
y_h4=highpass(y4,200,fs);
fref=fs*(1:referenceLength)/referenceLength;
R4=fft(y_h4,referenceLength);
stor4=R4(1);
R4(1)=0;
absR4=abs(R4);
plot(fref,absR4,'DisplayName','office','color','c');
hold on;
%6
[y5,fs]=audioread('hallway.mp3');
y_h5=highpass(y5,200,fs);
fref=fs*(1:referenceLength)/referenceLength;
R5=fft(y_h5,referenceLength);
stor5=R5(1);
R5(1)=0;
absR5=abs(R5);
plot(fref,absR5,'DisplayName','hallway','color','m');
hold on;
%7
[y6,fs]=audioread('ICU.mp3');
y_h6=highpass(y6,200,fs);
fref=fs*(1:referenceLength)/referenceLength;
R6=fft(y_h6,referenceLength);
stor6=R6(1);
R6(1)=0;
absR6=abs(R6);
plot(fref,absR6,'DisplayName','ICU','color','[0.63 0.078 0.18]');
hold on;
%8
[y7,fs]=audioread('music.mp3');
y_h7=highpass(y7,200,fs);
fref=fs*(1:referenceLength)/referenceLength;
R7=fft(y_h7,referenceLength);
stor7=R7(1);
R7(1)=0;
absR7=abs(R7);
plot(fref,absR7,'DisplayName','Music','color','[0.49 0.18 0.56]');
hold on;
%9
[y8,fs]=audioread('room.mp3');
y_h8=highpass(y8,200,fs);
fref=fs*(1:referenceLength)/referenceLength;
R8=fft(y_h8,referenceLength);
stor8=R8(1);
R8(1)=0;
absR8=abs(R8);
plot(fref,absR8,'DisplayName','room','color','[0.92 0.69 0.12]');
hold on;
legend;

Accepted Answer

Walter Roberson
Walter Roberson on 16 May 2021
Your code is assuming that the audio only has one channel, but instead it has two channels in each file.
  5 Comments

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by