# FFT alternates '0' for a quantized SIN wave

1. ## FFT alternates '0' for a quantized SIN wave

Hello,

Doing FFT exercises in Matlab I've discovered that for some frequencies (defined by k) the FFT of quantized SIN wave alternates '0'.
For k I used primary numbers.
With k = 11, 13, 17, 19, 23, 29, 31, 37 it's Ok
With K = 3, 5, 7, 41, 53, 67, 101, 127 FFT alternates '0'.
Any ideas ?

Pavel.

Code:
```close all;
% clear all;

L = 2^14;
res = 10;
A = 1;

Fs = 1e+5;
k = 41;
f1 = k/L*Fs;

t = (0:L-1)/Fs;
y = A*sin(2*pi*f1*t);

% Display Original Signal
figure(1)
plot(t, y);
grid on;

% Quantization
dA = (2*A)/2^res;
partition = (-A+dA:dA:A-dA);
codebook = (0:2^res-1);
[index,yq] = quantiz(y,partition,codebook);
yq = (yq - 2^(res-1))/2^res*2*A;

% Display Quantized Signal
figure(2);
plot(t,yq);
grid on;

% Display Quantization Error
figure(3);
plot(t, y-yq);
grid on;

P2 = abs(fft(yq, L))/L;
P1 = P2(1:L/2+1);
P1(2:end-1) = 2*P1(2:end-1);
f = Fs*(0:(L/2))/L;

% Display FFT
figure(4);
plot(f, mag2db(P1));
grid on;```

•

2. ## Re: FFT alternates '0' for a quantized SIN wave

Here is resume of FFT calculations for different values of k (picked from primary numbers): y - FFT Ok, n - FFT alternates '0'.

•

3. ## Re: FFT alternates '0' for a quantized SIN wave

Resolved.
The problem was in erroneous quantization.
Here is the correct quantization code:

% Quantization
dy = (2*A)/(2^res-1);
partition = (-A+A/2^res:dy:A-A/2^res);
codebook = (0:2^res-1);
[index,yq] = quantiz(y,partition,codebook);
yq = 2*A*yq/(2^res-1)-A;

where:
y - SIN waveform values
A - amplitude of SIN waveform
res - resolution of quantizer

--[[ ]]--