FFT of quantized signal

41 views (last 30 days)
Maruthi prasanna C
Maruthi prasanna C on 2 Feb 2020
Commented: Chunru on 11 Aug 2021
I am having difficulty in plotting FFT of quantized signal of an ideal qunatizer in MATLAB . When I plot the FFT in dB Scale , I am getting infinity as the value & figure is blank . Please help me, I have posted the Code below
fs = 1e6;
fx = 50e3;
Afs = 1;
N = 2^11;
% time vector
t = linspace(0, (N-1)/fs, N);
% input signal
y = Afs * cos(2*pi*fx*t);
% spectrum
s = 20 * log10(abs(fft(y)/N/Afs*2));
% drop redundant half
s = s(1:N/2);
% frequency vector (normalized to fs)
f = (0:length(s)-1);
figure(1)
plot(y);
title('input signal ')
ylabel('y');
xlabel('frequency bins');
B = 2; % no of bits
delta = 2/2^B;
fs = 1e6;
% Number of cycles in test
cycles = 67;
%Make N/cycles non-integer! accomplished by choosing cyclesÆprime #
%N=power of 2 speeds up analysis
% N = 2^10;
%signal frequency
fx = fs*cycles/N
y = Afs * cos(2*pi*fx*t);
s = 20 * log10(abs(fft(y)/N/Afs*2));
f = (0:length(s)-1);
figure(2)
plot(f,s);
title('spectrum of input signal ');
ylabel('y')
xlabel('frequency bins')
y1=round(y/delta)*delta;
figure(3)
plot(y,'b');
hold on
stem(y1,'r')
hold on
plot(y-y1,'g')
title(sprintf('Signal, Quantized signal and Error for %g bits, %g quantization levels',B,2^B));
xlabel('frequency');
hold off
y2=20*log10((abs(fft(y1)/N*2)));
figure(10)
f1 = (0:length(y2)-1);
plot(f1 ,y2)
  4 Comments
Chunru
Chunru on 11 Aug 2021
In theory, quantization can have any integer number of levels (not necessarily 2^B, though 2^B is prefered). The quantization can be non-uniform as well.

Sign in to comment.

Answers (2)

Chunru
Chunru on 11 Aug 2021
Quantizing the ideal sinusoidal signal will produce a perodical signal, which may have some weird spectrum with 0 at some frequencies causing the display problem. To study the quatization effect, you can use some other signals, for example chirp, pink noise. The following code add in an offset so that the spectrum is no longer "weird".
fs = 1e6;
fx = 50e3;
Afs = 1;
N = 2^11;
% time vector
t = linspace(0, (N-1)/fs, N);
% input signal
y = Afs * cos(2*pi*fx*t);
% spectrum
s = 20 * log10(abs(fft(y)/N/Afs*2));
% drop redundant half
s = s(1:N/2);
% frequency vector (normalized to fs)
f = (0:length(s)-1);
figure(1)
plot(y);
title('input signal ')
ylabel('y');
xlabel('frequency bins');
B = 2; % no of bits
delta = 2/2^B;
fs = 1e6;
% Number of cycles in test
cycles = 167;
%Make N/cycles non-integer! accomplished by choosing cyclesÆprime #
%N=power of 2 speeds up analysis
% N = 2^10;
%signal frequency
fx = fs*cycles/N
fx = 8.1543e+04
y = Afs * cos(2*pi*fx*t) + 0.123; % add a small offset
s = 20 * log10(abs(fft(y)/N/Afs*2));
f = (0:length(s)-1);
figure(2)
plot(f,s);
title('spectrum of input signal ');
ylabel('y')
xlabel('frequency bins')
y1=round(y/delta)*delta; y1'
ans = 2048×1
1.0000 1.0000 0.5000 0 -0.5000 -0.5000 -1.0000 -1.0000 -0.5000 0
figure(3)
plot(y,'b');
hold on
stem(y1,'r')
hold on
plot(y-y1,'g')
title(sprintf('Signal, Quantized signal and Error for %g bits, %g quantization levels',B,2^B));
xlabel('frequency');
hold off
y2=20*log10((abs(fft(y1)/N*2)))'
y2 = 2048×1
-13.8447 -58.6345 -62.4346 -74.3996 -103.6032 -59.0645 -60.1435 -59.3283 -45.3150 -38.9921
%y2=((abs(fft(y1)/N*2)))'
figure(10)
f1 = (0:length(y2)-1);
plot(f1 ,y2)

Yazan
Yazan on 11 Aug 2021
There are some issues with your code. See my modifications and comments below. You cannot calculate SNR, because you did not add noise to any signal (unless I'm missing something here).
clc, clear, close all
fs = 1e6;
fx = 50e3;
Afs = 1;
N = 2^11;
% time vector
t = linspace(0, (N-1)/fs, N);
% input signal
y = Afs * cos(2*pi*fx*t);
figure('Units', 'normalized', 'Position', [0.05 0.15 0.85 0.7])
subplot(3,2,1)
plot(t, y), title('Signal y'); axis tight
xlabel('Time - sec');
ylabel('Amplitude');
% spectrum
Nfft = N;
s = 1/Nfft * abs(fft(y, Nfft));
f = linspace(0, fs/2, Nfft/2+1);
subplot(3,2,2)
s = s(1:Nfft/2+1).^2;
sy = [s(1), 2*s(2:end-1), s(end)];
plot(f, pow2db(sy)), grid minor
title('One-sided power spectrum of y'), xlabel('Frequency - Hz');
ylabel('dB')
% or you can use the periodogram function with rect window
% you'll get exactly the same result
% [s2, f2] = periodogram(y, rectwin(length(y)), Nfft, fs, 'onesided', 'power');
% hold on, plot(f2, pow2db(s2))
% signal 2
B = 2;
delta = 2/2^B;
fs = 1e6;
cycles = 67;
fx = fs*cycles/N;
yy = Afs * cos(2*pi*fx*t);
subplot(3,2,3)
plot(t, yy), title('Signal yy'); axis tight
xlabel('Time - sec');
ylabel('Amplitude');
% spectrum
Nfft = N;
s = 1/Nfft * abs(fft(yy, Nfft));
s = s(1:Nfft/2+1).^2;
syy = [s(1), 2*s(2:end-1), s(end)];
subplot(3,2,4)
plot(f, pow2db(syy)), grid minor
title('One-sided power spectrum of yy'), xlabel('Frequency - Hz');
ylabel('dB')
% quantized signal
y1 = round(y/delta)*delta;
subplot(3,2,5)
n = 100;
plot(t(1:n), y1(1:n)), title('Signal y1'); axis tight
xlabel('Time - sec');
ylabel('Amplitude'); grid minor
hold on,
plot(t(1:n), y(1:n), '--')
err = y-y1;
plot(t(1:n), err(1:n))
legend({'Quantized', 'Original', 'Error'})
s = 1/Nfft * abs(fft(y1, Nfft));
s = s(1:Nfft/2+1).^2;
sy1 = [s(1), 2*s(2:end-1), s(end)];
subplot(3,2,6)
plot(f, pow2db(sy1)), grid minor
title('One-sided power spectrum of y1'), xlabel('Frequency - Hz');
ylabel('dB')
% estimate power
pRMSy = mag2db(rms(y));
pRMSyy = mag2db(rms(yy));
pRMSy1 = mag2db(rms(y1));
fprintf('Power of signal y = %g dBW\n', pRMSy)
Power of signal y = -3.01267 dBW
fprintf('Power of signal yy = %g dBW\n', pRMSyy)
Power of signal yy = -3.0103 dBW
fprintf('Power of signal y1 = %g dBW\n', pRMSy1)
Power of signal y1 = -2.22132 dBW

Tags

Community Treasure Hunt

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

Start Hunting!