I want to define inputs for the function quantized_signal

Looking at this function:
function quantized_signal = nonlinear_quantize(signal, bits, method, param)
end
How do I define signal, bits, method and param?

4 Comments

Normally, you would have code that called the function passing in appropriate parameters.
... Unless the question is about defining default values when parameters are not passed?
I needed an example of the two so that I can practice them to see the end result.
Could you please clarify what the {signal, bits, method, and param} are? We need to send these four inputs to the function nonlinear_quantize() in order to generate the output quantized_signal. If it is difficult to describe in words, please consider providing a sketch of the quantized_signal using the specified inputs {signal, bits, method, and param}.
Here is a code snippet:
% Sine wave function
function output_signal = sine_fcn(t, T, phi)
output_signal = sinpi(2/T*t + phi);
end
% user-defined parameters
T = 2; % time period to complete 1 oscillation
phi = 0; % phase angle in radian
t = linspace(0, 4, 4001);
% pass the inputs to sine_fcn()
x = sine_fcn(t, T, phi);
% plot the signal
plot(t, x), grid on
ylim([-1.5, 1.5])
title('Sine wave signal')
xlabel('Time, t')
ylabel('x(t)')
The code needs to be called as follows:
N = 512;
Signal = rand(1,N);
QS = nonlinear_quantize(Signal, 4, 'paisley_clown', 0.2198472);

Sign in to comment.

 Accepted Answer

Not sure if this is what you are looking for. I discovered that MATLAB has a function for performing quantization. However, it requires the Communications Toolbox. For more examples, please refer to the documentation for quantiz().
t = [0:0.1:4];
sig = sinpi(t);
partition = [-1.0:0.2:1.0];
codebook = [-1.2:0.2:1.0];
[index, quants] = quantiz(sig, partition, codebook);
plot(t, sig, 'x', t, quants,'.'), grid on
title('Quantization of Sine Wave')
xlabel('Time')
ylabel('Amplitude')
legend('Original sampled sine wave','Quantized sine wave');
axis([-0.2, 4.2, -1.5 1.5])

9 Comments

I think this answer is closer to what I wanted. Actually, I was learning about nonlinear quantization where μ-law compression (and similar A-law) applies non-uniform quantization to audio signals before digitization to better match human hearing characteristics. So I needed a graphical representation of how the nonlinear quantization will look like. I decided to get the code online and found out that there were errors with regards to the functions they defined. I have been able to rectify almost all of the errors (for mu_law_compress.m, mu_law_expand.m, a_law_compress.m and a_law_expand.m) in the function with the exception of this function:
Or do I need to take off the inputs (signal, bits, method, param) and replace it with something else to get the desired output?
At the end of my study, I want a graphical representation using matlab to better explain this process:
Analog signal → μ-law compression → Uniform quantization → Transmission/Storage→ Digital-to-analog → μ-law expansion → Reconstructed signal
but I want to narrow down my focus to just this two of the processes: μ-law compression → Uniform quantization
function quantized_signal = nonlinear_quantize(signal, bits, method, param)
end
We don't know. We do not have access to the error messages, and we do not know what values you are passing to the functions.
I'm unfamiliar with the A-Law. But you can possibly use the compand() function to implement the A-Law.
The function also requires the Communications Toolbox.
This is the code:
function quantized_signal = nonlinear_quantize(signal, bits, method, param)
% NONLINEAR_QUANTIZE - Perform nonlinear quantization
% signal: Input signal (vector)
% bits: Number of quantization bits
% method: 'mu-law' or 'a-law'
% param: μ value for mu-law (default 255) or A value for a-law (default 87.6)
if nargin < 4
if strcmpi(method, 'mu-law')
param = 255; % Default μ for telephony
else
param = 87.6; % Default A for A-law
end
end
% Normalize signal to [-1, 1]
max_val = max(abs(signal));
if max_val == 0
max_val = 1; % Avoid division by zero
end
normalized_signal = signal / max_val;
% Apply compression
switch lower(method)
case 'mu-law'
compressed = mu_law_compress(normalized_signal, param);
case 'a-law'
compressed = a_law_compress(normalized_signal, param);
otherwise
error('Method must be either "mu-law" or "a-law"');
end
% Perform uniform quantization on compressed signal
L = 2^bits; % Number of quantization levels
step_size = 2 / L; % Step size for [-1, 1] range
% Uniform quantization
quantized_compressed = floor((compressed + 1) / step_size) * step_size - 1 + step_size/2;
quantized_compressed = max(min(quantized_compressed, 1), -1); % Clamp
% Apply expansion (inverse compression)
switch lower(method)
case 'mu-law'
quantized_signal = mu_law_expand(quantized_compressed, param);
case 'a-law'
quantized_signal = a_law_expand(quantized_compressed, param);
end
% Scale back to original range
quantized_signal = quantized_signal * max_val;
end
% μ-law compression function
function compressed = mu_law_compress(x, mu)
compressed = sign(x) .* log(1 + mu * abs(x)) / log(1 + mu);
end
% μ-law expansion function
function expanded = mu_law_expand(y, mu)
expanded = sign(y) .* ((1 + mu).^abs(y) - 1) / mu;
end
% A-law compression function
function compressed = a_law_compress(x, A)
abs_x = abs(x);
compressed = zeros(size(x));
% A-law piecewise function
mask1 = abs_x <= 1/A;
mask2 = abs_x > 1/A;
compressed(mask1) = sign(x(mask1)) .* (A * abs_x(mask1)) / (1 + log(A));
compressed(mask2) = sign(x(mask2)) .* (1 + log(A * abs_x(mask2))) / (1 + log(A));
end
% A-law expansion function
function expanded = a_law_expand(y, A)
abs_y = abs(y);
expanded = zeros(size(y));
mask1 = abs_y <= 1/(1 + log(A));
mask2 = abs_y > 1/(1 + log(A));
expanded(mask1) = sign(y(mask1)) .* (abs_y(mask1) * (1 + log(A))) / A;
expanded(mask2) = sign(y(mask2)) .* exp(abs_y(mask2) * (1 + log(A)) - 1) / A;
end
And this is the error message I had:
function quantized_signal = nonlinear_quantize(signal, bits, method, param)
|
Error: Function definitions are not permitted in this context.
You cannot copy and paste functions into the MATLAB command line.
With the old version of MATLAB that you are using, it was also not possible to include function definitions in a script file.
First, save the individual function codes separately in the same folder as "nonlinear_quantize.m", "mu_law_compress.m", "mu_law_expand.m", "a_law_compress.m", and "a_law_expand.m".
Next, enter the following lines as shown below in the MATLAB Command Window. Once you are successful or feel comfortable, you can learn to create a script file.
% ------------------------------------------
% Enter these lines in MATLAB Command Window
% ------------------------------------------
t = linspace(0, 2, 2001);
x = sinpi(t); % signal
b = 8; % bits
m = 'a-law'; % method
p = 90; % parameter of the method
xq = nonlinear_quantize(x, b, m, p);
plot(t, [x; xq]), grid on
legend('continuous-time signal', 'A-law quantized signal')
ylim([-1.5, 1.5])
% -------------------------------------------------
% nonlinear quantization function
% -------------------------------------------------
function quantized_signal = nonlinear_quantize(signal, bits, method, param)
% NONLINEAR_QUANTIZE - Perform nonlinear quantization
% signal: Input signal (vector)
% bits: Number of quantization bits
% method: 'mu-law' or 'a-law'
% param: μ value for mu-law (default 255) or A value for a-law (default 87.6)
if nargin < 4
if strcmpi(method, 'mu-law')
param = 255; % Default μ for telephony
else
param = 87.6; % Default A for A-law
end
end
% Normalize signal to [-1, 1]
max_val = max(abs(signal));
if max_val == 0
max_val = 1; % Avoid division by zero
end
normalized_signal = signal / max_val;
% Apply compression
switch lower(method)
case 'mu-law'
compressed = mu_law_compress(normalized_signal, param);
case 'a-law'
compressed = a_law_compress(normalized_signal, param);
otherwise
error('Method must be either "mu-law" or "a-law"');
end
% Perform uniform quantization on compressed signal
L = 2^bits; % Number of quantization levels
step_size = 2 / L; % Step size for [-1, 1] range
% Uniform quantization
quantized_compressed = floor((compressed + 1) / step_size) * step_size - 1 + step_size/2;
quantized_compressed = max(min(quantized_compressed, 1), -1); % Clamp
% Apply expansion (inverse compression)
switch lower(method)
case 'mu-law'
quantized_signal = mu_law_expand(quantized_compressed, param);
case 'a-law'
quantized_signal = a_law_expand(quantized_compressed, param);
end
% Scale back to original range
quantized_signal = quantized_signal * max_val;
end
% -------------------------------------------------
% μ-law compression function
% -------------------------------------------------
function compressed = mu_law_compress(x, mu)
compressed = sign(x) .* log(1 + mu * abs(x)) / log(1 + mu);
end
% -------------------------------------------------
% μ-law expansion function
% -------------------------------------------------
function expanded = mu_law_expand(y, mu)
expanded = sign(y) .* ((1 + mu).^abs(y) - 1) / mu;
end
% -------------------------------------------------
% A-law compression function
% -------------------------------------------------
function compressed = a_law_compress(x, A)
abs_x = abs(x);
compressed = zeros(size(x));
% A-law piecewise function
mask1 = abs_x <= 1/A;
mask2 = abs_x > 1/A;
compressed(mask1) = sign(x(mask1)) .* (A * abs_x(mask1)) / (1 + log(A));
compressed(mask2) = sign(x(mask2)) .* (1 + log(A * abs_x(mask2))) / (1 + log(A));
end
% -------------------------------------------------
% A-law expansion function
% -------------------------------------------------
function expanded = a_law_expand(y, A)
abs_y = abs(y);
expanded = zeros(size(y));
mask1 = abs_y <= 1/(1 + log(A));
mask2 = abs_y > 1/(1 + log(A));
expanded(mask1) = sign(y(mask1)) .* (abs_y(mask1) * (1 + log(A))) / A;
expanded(mask2) = sign(y(mask2)) .* exp(abs_y(mask2) * (1 + log(A)) - 1) / A;
end
Okay, well noted.
I have been able to successfully generate the same output signal as what you have demonstrated above. Thanks for the guidelines.
It is good to hear that, @Albert. You can replace the "x" signal with your own signal and the define the input parameters according to your quantization specifications.
Okay, I will try that out.

Sign in to comment.

More Answers (1)

Are you asking in general about how to call functions in MATLAB? If so you probably want to look at this section of the Getting Started documentation. You may also want to work through the free MATLAB Onramp tutorial to quickly learn the essentials of MATLAB.
Are you asking what requirements this particular function has for its input arguments? As you've written it, the answer is none; there's no code in the body of the function so there's nothing to require that "this input is a positive scalar" or "that input is a square matrix", etc. [Of course, nothing in the (non-existent) code defines the variable quantized_signal that the function is supposed to return as output, so if you were to call this function with an output argument MATLAB would error.]
If you've snipped out the actual body for purposes of posting to MATLAB Answers, then you're going to have to look at the code, look at any help text or documentation the author has written describing the purpose/requirements of the input arguments, ask the author and/or maintainer for information on how to use this function, and/or try it out and deduce the requirements from any warning and/or error messages you receive when you call it with various inputs.

Products

Release

R2013a

Asked:

on 29 Jan 2026

Commented:

on 4 Feb 2026 at 2:23

Community Treasure Hunt

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

Start Hunting!