我写了以下代码:
freq=[261.626, 277.183, 293.665, 311.127, 329.628, 349.228, 369.994, 391.995,...
415.305, 440.000, 466.164, 493.883, 523.251, 554.365,...
587.330, 622.254, 659.255, 698.456, 739.989, 783.991, 830.609, 880.000,...
932.328, 987.767];
freq_length=length(freq);
notes=['C4 ';'C#4';'D4 ';'D#4';'E4 ';'F4 ';'F#4';'G4 ';'G#4';'A4 ';'A#4';...
'B4 ';'C5 ';'C#5';'D5 ';'D#5';'E5 ';'F5 ';'F#5';'G5 ';'G#5';'A5 ';'A#5';'B5 '];
notes_length=length(notes);
Duration=[1,1/2,1/4,1/8,1/16,1/32,1/64];
DurationLength=length(Duration);
DurationName=['whole Note','halfNote','QuarterNote','EighthNote','SixteenthNote',...
'ThirtySecondNote','SixtyFourNote'];
increases=[0,1/2,3/4,9/8];
IncreasesLength=length(increases);
increasesName=['ZeroDots','OneDot','TwoDots','ThreeDots'];
%endeiktikh timh sampleRate
SampleRate=1/20000;
%typikh timh (se sec gia thn whole note)
TimeValue=1.6;
TimeNotesSize=int64(IncreasesLength)*int64(DurationLength);
TimeNotes=zeros(TimeNotesSize,1);
%Ypologismos TimeValue gia kathe Duration,increases
for i=1:DurationLength
for j=1:IncreasesLength
TimeNotes(j+IncreasesLength*(i-1))=Duration(i)*(1+increases(j))*TimeValue;
%disp(j+IncreasesLength*(i-1));
end
end
for i = 1:TimeNotesSize
soundVector = zeros(notes_length,TimeNotesSize,int64(TimeNotes(i)/SampleRate));
end
for i = 1:1:notes_length % for every note
for j=1:TimeNotesSize %for every note duration
for k=0:SampleRate:TimeNotes(j) %for the SampleRate space
soundVector(i,j,int64(k/SampleRate)+1)=sin(2*pi*freq(i)*k);
end
end
end
%spectogram
y1=sin(2*pi*freq(1)*(0:SampleRate:TimeValue));
y2=sin(2*pi*freq(1)*(0:SampleRate:TimeNotes(1)));
y3=soundVector(1,1,:);
window=hamming(512);
noverlap=256;
nfft=1024;
[S,F,T,P]=spectrogram(y3,window,noverlap,nfft,Fs,'yaxis');
surf(T,F,10*log10(P),'edgecolor','none');
axis tight;
view(0,90);
set(gca,'clim',[-80,-30]);
xlabel('Time (seconds)');
ylabel('Frequency (Hz)');
主要目标是给出一首音乐歌曲,打印出这首歌的五角星音符。我可以打印y1
和y2
信号,但不能打印y3
信号。 y3
和y2
信号必须相同。
控制台返回的错误是:
Error using welchparse>segment_info (line 188)
The length of the segments cannot be greater than the length of the input signal.
Error in welchparse (line 32)
[L,noverlap,win] = segment_info(M,win,noverlap);
Error in spectrogram (line 172)
[x,nx,~,~,~,win,~,~,noverlap,~,~,options] = welchparse(x,esttype,varargin{:});
Error in play_notes (line 57)
[S,F,T,P]=spectrogram(y3,window,noverlap,nfft,Fs,'yaxis');
我做错了什么?
第一个错误:> [S,F,T,P]=spectrogram(y2,window,noverlap,nfft,1/SampleRate,'yaxis');
而不是[S,F,T,P]=spectrogram(y3,window,noverlap,nfft,Fs,'yaxis');
第二个错误:
y3=soundVector(1,1,1:int64(TimeNotes(1)/SampleRate));
代替y3=soundVector(1,1,:);
答案 0 :(得分:1)
我不知道为什么(:,:)是必要的,但[S,F,T,P]=spectrogram(y3(:,:),window,noverlap,nfft,1/SampleRate,'yaxis');
有效!