Adding salt and pepper noise using MATLAB - matlab

In an assignment, I have to add salt and pepper noise of specific SNR values to an image. How to achieve this using MATLAB ?
I know how to add salt and pepper noise with given noise density to an image (using imnoise)

"SNR" is not a well-defined term, there are many, many ways in which you can define SNR for a specific application.
So if we define SNR as the number of signal pixels divided by the number of noise pixels (which is not a weird definition in the case of salt and pepper noise, where there is no noise on some pixels, and no signal on other pixels), then it is quite straightforward to translate that SNR to noise density.
On the other hand, if you have a specific definition of SNR that you need to abide by, then you could use an iterative process as follows:
Pick a random pixel, and set it randomly to 0 or 1 (or whatever the max value is for your data type).
Compute the SNR according to your definition.
Go back to 1 if the computed SNR is larger than the required one.
This is obviously an expensive procedure...

Related

Adding noise to SIFT correspondances

I want to test under Matlab an algorithm for essential matrix estimation within ransac framework. The inputs are sift correspondances and several Ransac iterations are performed to select inliers from outliers (false correspondances).
I want to test the efficiency of the algorithm with different levels of noise.
should I only introduce mismatch to certain number of correspondances randomly? alternatively How to add noise to sift correspondances (formula)?
The simplest way is to take the set of key-points for all frames (the pixel coordinates of the features) and add Gaussian noise to their position for both x,y.
For example, if you have a matrix of key-points p of size nx2
noisy_p = p + sigma * randn(n,2);
where sigma is the standard deviation of noise you want to experiment with. Using a value of 1/2 or 1 gives typical noise you get on real-world images.

Matlab white noise signal with a maximum frequency

I need to generate a white noise signal with Matlab that has a maximum frequency of 5, 10 and 20 Hz. I know one way is to create the signal, then do a fourier transform, adjust the signal frequency and then inverse transform the signal back to the time domain. I can't really figure out how to do that in Matlab. Any help at all would be great
I need to generate a white noise signal with Matlab that has a maximum frequency of 5, 10 and 20 Hz.
That's the same as saying "I need a perfect circle, but it has to have exactly three edges". White noise doesn't have maximum frequencies; it's white because it spans all your bandwidth with the same expected noise energy.
Now, I presume you want to say "I want to have noise that is shaped a bit like signals that center around 5, 10 and 20 Hz". You'd still have to define the spectral shape -- but for the sake of argument, I assume you want to have gaussian shaped frequency response around these frequencies with a bandwidth of 2 Hz, with a sampling frequency of 50 Hz (has to be twice the highest frequency your signal has[in the real signal case], or else you'll get aliasing).
You can simply do that by using matlabs filter design toolbox, and applying the resulting filter to your signal. Usually, you'd just design one filter and shift it in frequency, but that doesn't seem to be your level of expertise yet -- which indicates that experimenting is an extremely good approach to becoming accustomed to DSP. Go wild!

Simulating noise in OFDM

Say I'd like to simulate a certain E_s/N0 in a baseband OFDM MATLAB modem for SER curves.
I randomize 50 QAM symbols with N subcarriers each in frequency domain, add null frequencies (guard bands) to each symbol, perform an IFFT and then add a CP.
Then I convolve the time domain signal with a certain channel impulse response with taps of my choice; I then want to add noise, but I'm not sure about how to normalize it so that it would have the right meaning of E_s/N0 (Energy per Symbol) and I'll get the theoretical results.
I get funky results with just MATLAB's agwn function with the 'measured' flag.
congratulations for being aware of this! The difference between SNR, E_b/N0 and E_s/N0 is far too often ignored.
The FFT on the OFDM receiver side will take the white noise (awgn) and spread it evenly over all bins.
What you thus see is the constant noise contribution to all symbols, be it CP, guard or information symbol, and is directly derived from N0 (and the FFT length).
The energy of your symbol depends on the QAM you're actually using -- QAM 16 with the same maximum amplitude as a QAM 1024 will have a different average symbol energy.
Since Parseval's theorem links frequency domain to time domain energy (depending on whether the FFT scales or doesn't with a factor of 1 or 1/N), so this will directly be your symbol energy.
Having both defined E_s and N0, the ratio of these should now be clear.

FFT: Match samples to frequency

let us assume,
I have a vector t with the times in seconds of my samples. (These samples are not equally distributed on the time domain.
Also I have a vector data containing the samplevalues at the time t.
t and data have the same length.
If I plot the graph some sort of periodical signal is obtained.
now I could perform: abs(fft(data)) to get my spectrum, which is then plotted over the amount of data points on the x-axis.
How can I obtain my spectrum regarding the times in vector t and plot it?
I want to see which frequencies in 1/s or which period in s my signal contains.
Thanks for your help.
[Not the OP's intention]: FFT will give you the spectrum (global) for any number of input data points. You cannot have a specific data point (in time) associated with parts (or the full) spectrum.
What you can do instead is use spectrogram and obtain the Short-Time Fourier Transform (STFT). This will give you a NxM discrete grid of time-frequency FT values (N: FT frequency bins, M: signal time-windows).
By localizing the (overlapping) STFT windows on your data samples of interest you will get N frequency magnitude values, thus the distribution of short-term spectrum estimates as the signal changes in time.
See also the possibly relevant answer here: https://stackoverflow.com/a/12085728/651951
EDIT/UPDATE:
For unevenly spaced data you need to consider the Non-Uniform DFT (and Non-uniform FFT implementations). See the relevant question/answer here https://scicomp.stackexchange.com/q/593
The primary approaches for NFFT or NUFFT, are based on creating a uniform grid through local convolutions/interpolation, running FFT on this and undoing the convolutional effect of the interpolation filter.
You can read more:
A. Dutt and V. Rokhlin, Fast Fourier transforms for nonequispaced data, SIAM J. Sci. Comput., 14, 1993.
L. Greengard and J.-Y. Lee, Accelerating the Nonuniform Fast Fourier Transform, SIAM Review, 46 (3), 2004.
Pippig, M. und Potts, D., Particle Simulation Based on Nonequispaced Fast Fourier Transforms, in: Fast Methods for Long-Range Interactions in Complex Systems, 2011.
For an implementation (with an interface to MATLAB) try NFFT and possibly its parallelized version PNFFT. You may find a nice walk-through on how to set-up and use here.
You can resample or interpolate your sample points to get another set of sample points that are equally spaced in t. The chosen spacing or sample rate of the second set of equally spaced sample points will allow you to infer frequencies to the result of an FFT of that second set.
The results may be noisy or include aliasing unless the initial data set is bandlimited to a sufficiently low frequency to allow interpolation. If bandlimited, then you might try something like cubic splines as an interpolation method.
Although it may look like one can get a high FFT bin frequency resolution by resampling to a larger number of data points, the actual useful resolution accuracy will be more related to the original number of samples.

white noise has flat power spectral density

I am trying to prove that the white noise has constant power spectral density using matlab
but the amplitude of the spectrum looks like random amplitude.
can anyone tell me why?
here is my code.
noise = randn(1,10000);
fft_noise=fft(noise);
plot(abs(fft_noise(1:5000)))
thanks.
You need to average a bunch (law of large numbers) of FFTs of white noise to approach the average power spectral density.
If you take the FFT of an independent set of random variables from the same distribution, then you'll get an independent set of random variables from the same distribution since the inverse Fourier transform is (more or less) the same as the Fourier transform. The point is that the expected value for each frequency is the same.
you need to multiple the fft by the complex conjugate of the fft to show a flat PSD. i.e. change
fft_noise=fft(noise);
to
fft_noise=fft(noise).*conj(fft(noise));