46

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?

herohuyongtao
  • 45,575
  • 23
  • 118
  • 159
geeti
  • 473
  • 1
  • 4
  • 6
  • possible duplicate of [normalize mat file in matlab](http://stackoverflow.com/questions/7729880/normalize-mat-file-in-matlab) – Jonas Jan 03 '12 at 19:05

5 Answers5

87

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(:));
Oli
  • 15,347
  • 7
  • 47
  • 62
  • 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 '14 at 16:18
  • 19
    \forall scalar a, std(x) == std(x+a) – Oli Mar 27 '14 at 04:47
13

If you have the stats toolbox, then you can compute

Z = zscore(S);
jon
  • 131
  • 1
  • 2
11

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.

6

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)
Kavka
  • 4,062
  • 14
  • 28
1

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
Yas
  • 3,443
  • 1
  • 29
  • 22