I'm trying to produce a sampled piano note and I keep getting the error "Index exceeds matrix dimensions." I'm not sure where i've gone wrong :/

6 ビュー (過去 30 日間)
Matthew
Matthew 2014 年 2 月 18 日
編集済み: Wayne King 2014 年 2 月 18 日
%% Part 1: Preparing the signal
[pianosig,Fs]=wavread('piano_A.wav'); %%Imports the piano signal
Length=length(pianosig); %%len = the length of the signal
Duration = Length/Fs; %%the duration of the signal is length divided by the sampling frequency
Ts=1/Fs; %%sampling time is 1 divided by the sampling frequency
Time=0:Ts:Duration-Ts; %%makes samples at set intervals
plot(Time,pianosig);xlabel('time (sec)'); %%plots the signal on a graph, x label is time in seconds
ylabel('Amplitude');title('Piano A time domain'); %%y label is amplitude
sptool %%View the spectrum information
sound(pianosig,Fs) %%play signal
%%Part 2: Harmonic Content
% Harmonics Frequency(Hz) Magnitude(dB)
% 1st 441 -34
% 2nd 880 -38
% 3rd 1325 -47
% 4th 1770 -50
% 5th 2222 -61
% 6th 2680 -70
% 7th 3144 -72
% 8th 3609 -86
% 9th 4087 -94
% 10th 4587 -103
% 11th 5069 -99
% 12th 5999 -92
% 13th 6999 -94
% 14th 7999 -96
% 15th 9000 -96
% 16th 10000 -98
% 17th 11000 -98
% 18th 12000 -101
%%Part 2: Creating the envelope
Temp = pianosig.^2;
plot(Temp);
d = fdesign.lowpass(40,100,1,80,44100);
Lowpass=design(d,'kaiserwin');
Envelope=filter(Lowpass,Temp);
plot(Envelope);
%%Part 3: Creating the sampled signal
Frequencies = [441, 880, 1325,1770, 2222, 2680, 3144, 3609, 4087, 4587, 5069, 5999, 6999, 7999, 9000, 10000, 11000, 12000];
Magnitude = [-34, -38, -47, -50, -61, -70, -72, -86, -94, -103, -99, -92, -94, -96, -96, -98, -98, -101];
Magnitude = db2mag(Magnitude); %%converts dB to magnitude values
Signal=0;
for i = 1:length(Frequencies) %%creating sine waves and adding them together
Signal = Signal+Magnitude(i)*sin(Frequencies(i)*2*pi*Time);
end
SynthSignal = Signal(1:length(Envelope)).*Envelope; %%applies envolope as a guide
SynthSignal=SynthSignal./max(abs(SynthSignal)); %%Normalises the signal
plot(SynthSignal);
sound(SynthSignal,Fs);

回答 (1 件)

Wayne King
Wayne King 2014 年 2 月 18 日
編集済み: Wayne King 2014 年 2 月 18 日
Without commenting on your code, the following should fix your problem
for i = 1:length(Frequencies)
Signal = Signal+Magnitude(i)*sin(Frequencies(i)*2*pi*Time);
end
Signal = Signal(:);
SynthSignal = Signal(1:length(Envelope)).*Envelope; %%applies envolope
Note that I have inserted
Signal = Signal(:);
after your for loop exits.

Community Treasure Hunt

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

Start Hunting!

Translated by