```Date: Jan 5, 2013 11:25 PM
Author: anyone
Subject: Re: SNR script to run using Matlab

On Fri, 04 Jan 2013 09:27:08 +0000, Wan  wrote:> anyone <ieGJnr4aQXRb!uzEL@z³> wrote in message> <50e37605\$0\$46968\$c3e8da3\$38634283@news.astraweb.com>...>> On Tue, 01 Jan 2013 20:41:08 +0000, Wan  wrote:>> >> > Hi,>> > >> > I have a problem to run a script for SNR. Here the details.>> > >> > By using the given signal, the signal should be embended with noise>> > with with different Signal-to-noise ration (SNR) of 0dB, 5dB, 10dB, >> > 15dB, 20dB, 25 dB and 30 dB.  Details of the programming should>> > include: 1.	How SNR of the noisy signal is calculated 2.	Comparison>> > of time domain and Power Spectrum Density (PSD) of original signal,>> > noisy signal and filtered signal for each SNR.>> > >> > Anyone know how to write script for this SNR using given signal?>> >> Yes, I can think of a few ways one could do that.  First, you should>> get started on your homework, then come back with an update on what you>> tried to do and what actually happened.> Hi,> > below is my coding:> > %<--- LOAD SIGNAL (FILENAME:mydata.mat)> %data = ('mydata.mat')> Fs=100;> t= 0: 1/Fs: 20;> S = load('mydata.mat');> signal = S.td;> x= signal SNR = 0;> % Generate Gaussian Noise with zero mean and unit variance %function [y]> = add_noise (signal, SNR)> x_var=var (x);> x_lng=length(x)> > n_var=x_var/10^(SNR/10);> noise=n_var*randn(1,x_lng);> > %mix signal with noise mix = x+ noise;> > %SNR equation %SNR dB= 20 log10 (STDsignal/STDnoise)> %STDs = std(signal)> %STDn = std(noise)> %SNR_measured_dB = 20* log(STDs/STDn)> > Fc = 100 ; %cut off frequency> > w = Fc/(2*Fs); % Normalized frequency %design a low pass fitler with> above mentioned specifications [b,a]=butter(2,w,'low'); %5th order> butterworth LPF [h,w]=freqz(b,a,1024); %Frequency response of the filter> > y=filter(b,a,mix); % pass the input signal through the filter % y is> your output.> > %subplot figure (1)> figure(1);> %subplot(4,1,1);> plot(t, signal);> title('Input Signal');> > %subplot(4,1,2);> %plot(t, noise);> %title('Generated Noise');> > %subplot(4,1,3);> figure(2);> plot(t, mix);> title(['Signal + Noise for SNR = 30 dB']);> > figure(3);> %subplot(4,1,4);> plot(t, y);> title('Filtered Signal');> > %Plot the Frequency response of the filter (Optional)> %subplot(5,1,5);> figure(4);> g = length(w/pi*2*Fs)> i = length(abs(h))> plotHandle=plot(w/pi*2*Fs,abs(h));> set(plotHandle,'LineWidth',2.5);> title('Frequency Response of a 5th Order Butterworth LPF');> xlabel('Frequency (Hz)')> ylabel('Magnitude');> grid;> > %plotting psd figure (5)> %subplot(4,1,1);> x = signal;> NFFT = 100;> X = fft(x, NFFT);> plot(-NFFT/2:NFFT/2 - 1, fftshift(abs(X)))> title('PSD Input Signal');> > figure(6);> %subplot(4,1,2);> v = noise;> NFFT = 100;> V = fft(v, NFFT);> plot(-NFFT/2:NFFT/2 - 1, fftshift(abs(V)))> title('PSD Generated Noise');> > %subplot(4,1,3);> figure(7);> w = mix;> NFFT = 100;> W = fft(w, NFFT);> plot(-NFFT/2:NFFT/2 - 1, fftshift(abs(W)))> title(['PSD Signal + Noise for SNR= 30 dB']);> > %subplot(4,1,4);> figure(8);> m = y;> NFFT = 100;> M = fft(m, NFFT);> plot(-NFFT/2:NFFT/2 - 1, fftshift(abs(M)))> title('PSD Filtered Signal');Bunch of odd stuff in that.  Was there some kind of question?
```