Im a new MATLAB user. Trying to initialize y to the calculated value in the if statement. However, when I try to plot y it says y not defined
function [y,xmax] = Alaw(x,A,ymax)
if nargin<3, ymax=1; end
if nargin<2, A=87.6; end
xmax=max(abs(x));
temp = ymax/A;
if ((x > 0) & ( x < temp ))
y = (A * abs(x)) ./ (1 + log(A) ).*sign(x);
end
if (x > temp)
y = ymax*( 1 + log(A*abs(x/ymax)) )./( 1+log(A) ).*sign(x);
end
fprintf('Plotting Data ...\n');
hold on;
figure;
plot(y);
xlabel('x-axis');
ylabel('y-axis');
title(' A LAW ');
pause;
fprintf('Writing the audio file ...\n');
wavwrite(y, 22050, 'Alaw.wav');
end
If
xis smaller than or equal to zero, or ifxequalstemp, neither of the if-statements is true, and thus,ynever gets defined. You may want to re-write the logic withif..elseif..elseto ensure thatygets assigned in every case:Note that
x>0can lead to unexpected results whenxis an array. Usealloranyto make sure that the condition is satisfied if either all or any of the elements ofxsatisfy the condition, respectively.