% 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:Zif ( 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);
endif ( 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);endendsound(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)
MATLAB: 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
audiodigital signal processingsignal processing
Best Answer