50

I am new to MATLAB and I am trying to built a voice morphing system using MATLAB.

So I would like to know how to normalize a signal to zero mean and unit variance using MATLAB?

1

5 Answers 5

93

if your signal is in the matrix X, you make it zero-mean by removing the average:

X=X-mean(X(:));

and unit variance by dividing by the standard deviation:

X=X/std(X(:));
2
  • 3
    one remark/question @Oli, in your code, you're actually computing the std of the aligned/zero-mean data (x-mu), i.e: std(x-mu), but it should be: std(x), right?
    – Tin
    Mar 25, 2014 at 16:18
  • 20
    \forall scalar a, std(x) == std(x+a)
    – Oli
    Mar 27, 2014 at 4:47
14

If you have the stats toolbox, then you can compute

Z = zscore(S);
12

You can determine the mean of the signal, and just subtract that value from all the entries. That will give you a zero mean result.

To get unit variance, determine the standard deviation of the signal, and divide all entries by that value.

9

It seems like you are essentially looking into computing the z-score or standard score of your data, which is calculated through the formula: z = (x-mean(x))/std(x)

This should work:

%% Original data (Normal with mean 1 and standard deviation 2)
x = 1 + 2*randn(100,1);
mean(x)
var(x)
std(x)

%% Normalized data with mean 0 and variance 1
z = (x-mean(x))/std(x);
mean(z)
var(z)
std(z)
2

To avoid division by zero!

function x = normalize(x, eps)
    % Normalize vector `x` (zero mean, unit variance)

    % default values
    if (~exist('eps', 'var'))
        eps = 1e-6;
    end

    mu = mean(x(:));

    sigma = std(x(:));
    if sigma < eps
        sigma = 1;
    end

    x = (x - mu) / sigma;
end

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.