How can I make gaussian noise with certain mean value in simulink? - matlab

How can I make gaussian noise with mean= 18 and variance= 0.1 in simulink? I can't use AWGN block since I'm not able to specify mean value in it.
I want to generate the below signal which is gaussian noise with mean= 18 and variance= 0.1:

Construct the model as follows. From the Library Browser select the DSP System Toolbox, then choose the Random Source block.
Now, adjust the block parameters Source type, Mean and Variance:
Construct the model by adding a Scope block as follows:
After running the model for 10 sec., you will see the generated Gaussian noise waveform by double-clicking the Scope block. You will notice the plot is not similar to the image you posted, this is how the default plot works in Simulink.
Now, to make sure it's the correct signal, send the signal to MATLAB using the To Workspace block and plot it. This is how this noise waveform is plotted in MATLAB, the same as your image.

The mean just says how much the noise is shifted so if you take a constant function of value 18 and then add a gaussian noise of variance .1 you will get what you want.

This is assuming your noise is one-dimensional:
variance = 0.1;
std_deviation = sqrt(variance);
mean = 18;
n = 1000; % number of samples
noise = std_deviation .* randn(n, 1) + mean;

Related

Gaussian pulse generator block issue

i'm new in Simulink and I'm using interpreted MATLAB function block to create a gaussian pulse generator.
This is the function:
function y=mono_gauss(t)
fs=20E9; %sample rate-10 times the highest frequency
ts=1/fs; %sample period
t1=.5E-9; %pulse width(0.5 nanoseconds)
x=(t/t1).*(t/t1); %x=(t^2/t1^2)(square of (t/t1);
A=1;
y=(A*(t/t1)-ts).*exp(-x); %first derivative of Gaussian pulsefunction
end
The problem is that the output of the block generate only one pulse and my objective is to generate a train of pulses just like a pulse generator block.
Any solutions ?
You're most likely better off designing your pulse in MATLAB, then using the Repeating Sequence to use it in Simulink.
For instance, in MATLAB
t = 0:0.01:1;
y = normpdf(t,0.5,0.05);
plot(t,y)
Then within Simulink,
I have also changed the step size of the model Solver to be 0.01.
You'll need to play around with various of these parameters to get the exact curve you desire.

Random noise and DC component

I am using the following code to plot a Random noise as follows:
n=0:1/250:1;
random_noise=rand(size(n));
N=length(b);
f_bins=0:N-1;
N_2=ceil(N/2);
f_hertz=f_bins*fs/N;
figure
ll=abs(b);
plot(f_hertz(1:N_2),ll(1:N_2))
title('amplitude spectra of random signal')
The random noise assumes zero mean so why the random noise has a DC component as shown in the above figure? Also a general question, is there is a way to remove the DC component without the use of a filter?
If you read the documentation to rand, you'll see that it generates numbers from a uniform distribution in the range [0,1). The mean for this distribution is 0.5. So simply subtracting 0.5 from your signal will remove the D.C. component:
random_noise = rand(size(n)) - 0.5;
On the other hand, you probably want to use randn instead, which creates a 0-mean normal distribution.
In general, subtracting the mean from a signal removes the D.C. component:
signal = signal - mean(signal);
BTW: typing help rand in MATLAB will also show you the documentation for the function. If you're wondering why rand does something your don't expect, reading the documentation should be your first step.

Matlab: computing signal to noise ratio (SNR) of two highly correlated time domain signals

I'm working in the space of biosignal acquisition. I made a experiment as detailed below, and am now trying to obtain some results from the data.
I have a text file of a signal in Matlab. I loaded the signal onto a waveform generator, then I recorded the generator output on an oscilloscope.
I imported the recorded signal from the oscilloscope back into Matlab.
The Pearson's correlation coefficient between the original signal and the oscilloscope signal is 0.9958 (obtained using corrcoeff function).
I want to compute the SNR of the oscilloscope signal (what I'm calling my signal plus whatever noise is introduced through the digital-to-analog conversion and visa-versa). I have attached a snippet of the 2 signals for reference.
So my original signal is X and oscilloscope signal is X + N.
I used the snr function to compute SNR as follows.
snr(original, (oscilloscope - original))
The result I got was 20.44 dB.
This seems off to me as I would have thought with such a high correlation, that the SNR should be much higher?
Or is it not appropriate to try and compute SNR in this sort of situation?
All help is appreciated.
Thanks
Edit: Graph of a couple of results vs Sleutheye's simulated relationship
You might be surprised at just how even such moderate SNR can still result in fairly high correlations.
I ran an experiment to illustrate the approximate relation between correlation and signal-to-noise-ratio estimate. Since I did not have your specific EEG signal, I just used a reference constant signal and some white Gaussian noise. Keep in mind that the relationship could be affected by the nature of the signal and noise, but it should give you an idea of what to expect. This simulation can be executed with the following code:
SNR = [10:1:40];
M = 10000;
C = zeros(size(SNR));
for i=1:length(SNR)
x = ones(1,M);
K = sqrt(sum(x.*x)/M)*power(10, -SNR(i)/20);
z = x + K*randn(size(x));
C(i) = xcorr(x,z,0)./sqrt(sum(x.*x)*sum(z.*z));
end
figure(1);
hold off; plot(SNR, C);
corr0 = 0.9958;
hold on; plot([SNR(1) SNR(end)], [corr0 corr0], 'k:');
snr0 = 20.44;
hold on; plot([snr0 snr0], [min(C) max(C)], 'r:');
xlabel('SNR (dB)');
ylabel('Correlation');
The dotted black horizontal line highlights your 0.9958 correlation measurement, and the dotted red vertical line highlights your 20.44 dB SNR result.
I'd say that's a pretty good match!
In fact, for this specific case in my simulation (x = 1; z = x + N(0,σ)) if we denote C(x,z) to be the correlation between x and z, and σ as the noise standard deviation, we can actually show that:
Given a correlation value of 0.9958, this would yield an SNR of 20.79dB, which is consistent with your results.

how to find standard deviation of noise in an image using matlab?

As a part of my project, initially find low resolution of the input image. Then as a second step i need find the noise in the low-resolution image. How to find noise in an image and its standard deviation using matlab?
You can use the std matlab function which returns the standard deviation of a matrix.
std_deviation = std(image);
This will give you the standard deviation of the whole image. However you cannot calculate the noise std since you don't have the original filtered image.
Possible solution: (Not accurate) : This suppose thaht your noise is gaussian
Well, you can render several Noise matrices and test them:
(choose your mean_vector and std_vector)
for i = 1 : length(mean_vector) % or length(std_vector)
Noise(:,:,i) = mean_vector(i) + std_vector(i).*randn(size(your_image))
% extracting the possibly filtered image
filtered_img(:,:,i) = your_image - Noise(:,:,i);
end
Then display every filtered_img and choose the one that looks the less noisy.
You can denoise the image, compute the difference between the raw image and the denoised version, and then compute the standard deviation of the difference.
For instance:
a=imread('input');
a=double(a);
b=imsharpen(a); %you may need to tune the parameters
diff=b-a;
noise=std2(diff);
You can find the variance of noise in the image assuming that you know the distribution. You will know this if you have read some of the great works in image denoising field by Donoho & John.
To find the noise std. dev. of noise in an image with Gaussian contamination (additive), you can use the Median Absolute Deviation (MAD) estimator on the derivative of the image using the following kernel:
I am writing the python code for this, you can easily write it in Matlab:
def find_stddevs(img):
k = np.asmatrix([[-1.0/9, -1.0/9 ,-1.0/9],
[-1.0/9, 8.0/9 , -1.0/9],
[-1.0/9, -1.0/9 ,-1.0/9]])
filtered = convolve(img,k,mode='reflect')
median_a = np.median(filtered)
stddev = np.median(np.absolute(filtered - median_a))/0.67449 #Gaussian noise assumption
return stddev
You can see the derivation and the logic Here on Wikipedia.
Again, most people think it can be done. This is true iff you do not have any prior about the type of contamination in the image.

making pink noise (1/f) using list of frequencies

making pink noise (1/f) using list of frequencies
I would like to see what type of noise I would get if I used just the frequency in my voice. I created a matlab/octave array using fft to get the [frequency,amplitude,phase] to reproduce my vocal signal.
I would like to take this file/data and use it to create pink noise (1/f). Of course when I use 1/f for the frequency the numbers become very small does anyone have any ideas how to use my own vocal frequencies that I get from doing a fft in matlab to create pink noise (1/f).
Thanks
If I am correct, what you are doing is generating noise based on a 1/f frequency. However if you read the following article: http://en.wikipedia.org/wiki/Pink_noise the frequencies are the same except that the power spectral density is S is proportional to 1/f. Hence you should not be generating noise of frequency 1/f.
I would suggest reading this page for the necessary algorithms.
However if the problem you are facing is that the volume is too low, try amplifying the synthesized noise by multiplying the result by a factor ex.: pinkNoise = pinkNoise * 100
This might do the trick: compute the mean power in your spectrum from the amplitude A = A(f), where f is the frequency.
P = mean(A.^2);
Spread that over your frequency range:
N = length(f);
invfnorm = 1./[1:N];
Anew = sqrt(P*invfnorm/sum(invfnorm));
Anew has the property of having the same total power density as the original spectrum, and power decaying as 1/f.
Substitute Anew for A and inverse FFT your new spectrum to generate the new waveform.