I'm getting the error that Vectors must be the same lengths. Error in plot (x_axis, signal); I'm unsure as to how to change the length of the vectors?

1 view (last 30 days)
% valve.m
% Add harmonics to sinusoidal input using non-linear transform function
% Show output waveform for sinusoidal input and FFT
% Original Written by: Richard Sikora. 29th November 2004
% Modified to input a sound wave and output distorted signal
Z =120e3;
x = 0 : 4 * pi/Z : 4 * pi;
signal = wavread('di guitar_01.wav'); % sample in soundclip
sound(signal,44e3); % plays original soundclip
% Try changing gain from 1.000 in line below in range 0.000 to 1.000
%signal = 1.0000 * sin(x);
x_axis = 0 : 1/(Z-1) : 1.000 ;
% Coefficients y = a * x + b * x ^ 3
% Coefficients a and b adjust the amount of distortion present
a = 2.00;
b = 100;
for i = 1:Z
if ( signal(i) >= 0.0 )
output(i) = a * signal(i) - b * signal(i) * signal(i); % subs nonlinear distortion
%y(i) = a * x_axis(i) - b * x_axis(i) * x_axis(i);
end
if ( signal(i) < 0.0)
output(i) = a * signal(i) + b * signal(i) * signal(i); % adds nonlinear distortion
%y(i) = a * x_axis(i) - b * x_axis(i) * x_axis(i);
end
end
sound(output,44e3); % plays modified soundclip
subplot (3,1,1) %plot input wave
plot (x_axis, signal);
title ('Sampled Signal');
ylabel ('Magnitude');
subplot (3,1,2);
plot ( x_axis, output ), grid on; % plot output wave
title ('Valve Sound Output for Sine Input');
ylabel ('Magnitude');
subplot (3,1,3);
y = fft(output); % fft of output signal so we can see frequency spectrum of signal
harmonic = abs(y(1:25))/ abs(y(3));
xaxis = 0 : 0.5 : 25;
stem ( xaxis(1:25), harmonic(1:25)); % plot frequency spectrum of output wave
title ('Harmonic Content');
ylabel ('Magnitude');
set (gca, 'XTick', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]);
xlabel ('Harmonic');
% shows harmonics (frequency content) in the output wave
Harmonic_1 = harmonic(3)
Harmonic_2 = harmonic(5)
Harmonic_3 = harmonic(7)
Harmonic_4 = harmonic(9)
Harmonic_5 = harmonic(11)
Harmonic_6 = harmonic(13)
Harmonic_7 = harmonic(15)
Harmonic_8 = harmonic(17)

Accepted Answer

Star Strider
Star Strider on 8 Aug 2015
Since ‘signal’ appears to be the governing size, define ‘x_axis’ as:
x_axis = linspace(0, 1, length(signal));
That will make them equal length.
  2 Comments
tiernan brennan
tiernan brennan on 9 Aug 2015
Edited: Walter Roberson on 9 Aug 2015
no im still getting the same error
% Warren Tsu and Earvin Caceres
% EE113D Final Project
% valve.m
% Add harmonics to sinusoidal input using non-linear transform function
% Show output waveform for sinusoidal input and FFT
% Original Written by: Richard Sikora. 29th November 2004
% Modified to input a sound wave and output distorted signal
Z =120e3;
x = 0 : 4 * pi/Z : 4 * pi;
signal = wavread('di guitar_01.wav'); % sample in soundclip
sound(signal,44e3); % plays original soundclip
% Try changing gain from 1.000 in line below in range 0.000 to 1.000
%signal = 1.0000 * sin(x);
x_axis = linspace(0, 1, length(signal));
% Coefficients y = a * x + b * x ^ 3
% Coefficients a and b adjust the amount of distortion present
a = 2.00;
b = 100;
for i = 1:Z
if ( signal(i) >= 0.0 )
output(i) = a * signal(i) - b * signal(i) * signal(i); % subs nonlinear distortion
%y(i) = a * x_axis(i) - b * x_axis(i) * x_axis(i);
end
if ( signal(i) < 0.0)
output(i) = a * signal(i) + b * signal(i) * signal(i); % adds nonlinear distortion
%y(i) = a * x_axis(i) - b * x_axis(i) * x_axis(i);
end
end
sound(output,44e3); % plays modified soundclip
subplot (3,1,1) %plot input wave
plot (x_axis, signal);
title ('Sampled Signal');
ylabel ('Magnitude');
subplot (3,1,2);
plot ( x_axis, output ), grid on; % plot output wave
title ('Valve Sound Output for Sine Input');
ylabel ('Magnitude');
subplot (3,1,3);
y = fft(output); % fft of output signal so we can see frequency spectrum of signal
harmonic = abs(y(1:25))/ abs(y(3));
xaxis = 0 : 0.5 : 25;
stem ( xaxis(1:25), harmonic(1:25)); % plot frequency spectrum of output wave
title ('Harmonic Content');
ylabel ('Magnitude');
set (gca, 'XTick', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]);
xlabel ('Harmonic');
% shows harmonics (frequency content) in the output wave
Harmonic_1 = harmonic(3)
Harmonic_2 = harmonic(5)
Harmonic_3 = harmonic(7)
Harmonic_4 = harmonic(9)
Harmonic_5 = harmonic(11)
Harmonic_6 = harmonic(13)
Harmonic_7 = harmonic(15)
Harmonic_8 = harmonic(17)
Star Strider
Star Strider on 9 Aug 2015
You might need to do the same with all the files that derive from or are used with your ‘signal’ array. If signal is a two-channel signal with size (Nx2), then x_axis must also be a column vector of size (Nx1).
I can’t run your code and test it without your 'di guitar_01.wav' file. If converting the other vectors to column vectors doesn’t solve the problem, please attach your file (using the ‘paperclip’ button and completing both steps to upload it).

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 9 Aug 2015
What if Z is longer than your signal?
What if the signal read in has multiple channels?
The number of samples for something that has been read in by wavread() is size(signal,1) not necessarily length(signal)
You should not be using linspace(0,1,N) to define your x axis. You should be telling wavread() to return FS as well and your x axis should be (1:number_of_samples)/FS

Community Treasure Hunt

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

Start Hunting!