Sunday, 20 December 2015

matlab - Calculating SNR from Frequency Domain


As I am newbie to signals, I have collected an appliance signal in frequency domain as follows: enter image description here


(PSD- Power Spectral Density). So, I need to calculate the SNR (Signal to Noise Ratio) and BER (Bit Error Rate) caused by this signal in Powerline Calculation. So, I have made these codes in Matlab, but what I am stuck with is calculating the SNR and BER.


bits=10000; %number of bit

b=randi([0,1],1,bits); % generate random [0,1]
t=0:1/30:1-1/30; % Time period allocated for the signal

%ASK Carrier Signals
carrier_signa_l= sin(2*pi*t);
E1=sum(carrier_signa_l.^2);
carrier_signa_l=carrier_signa_l/sqrt(E1); %unit energy
carrier_signal_0 =0 * sin(2*pi*t); % zeros for 0 bits in the carrier signal

%MODULATION

ask=[];
for i=1:bits
if b(i)==1 % If bit = 1
ask=[ask carrier_signa_l];
else
ask=[ask carrier_signal_0];
end
end

Answer



If it is a sinusoidal signal, there will be peak (among the frequency bins) in the frequency spectrum corresponding to the tone's frequency.



Ratio of the magnitude of this peak to the sum of the magnitudes of all other bins (which are noise) correspond to Signal to Noise Ratio.


But when its a non sinusoidal signal (like the one in your plot) you have to consider the relevant band of the signal instead of a single peak. you can quantify it by specifying a frequency bin corresponding to the mojor frquency component in the signal plus some leakage (related to bandwidth) into the nearby bins.


Please take a look in the following matlab code.


N               = 8192; % FFT length
leak = 50;
% considering a leakage of signal energy to 50 bins on either side of major freq component

fft_s = fft(inptSignal,N); % analysing freq spectrum
abs_fft_s = abs(fft_s);


[~,p] = max(abs_fft_s(1:N/2));
% Finding the peak in the freq spectrum

sigpos = [p-leak:p+leak N-p-leak:N-p+leak];
% finding the bins corresponding to the signal around the major peak
% including leakage

sig_pow = sum(abs_fft_s(sigpos)); % signal power = sum of magnitudes of bins conrresponding to signal
abs_fft_s([sigpos]) = 0; % making all bins corresponding to signal zero:==> what ever that remains is noise
noise_pow = sum(abs_fft_s); % sum of rest of componenents == noise power


SNR = 10*log10(sig_pow/noise_pow);

No comments:

Post a Comment

readings - Appending 内 to a company name is read ない or うち?

For example, if I say マイクロソフト内のパートナーシップは強いです, is the 内 here read as うち or ない? Answer 「内」 in the form: 「Proper Noun + 内」 is always read 「ない...