generate white noise in matlab interval [-1 1] [duplicate] - matlab

This question already has answers here:
Generate white noise with amplitude between [-1 1] with Matlab
(6 answers)
Closed 9 years ago.
I would like to create or generate white noise in the range of [-1 1], but I don't know exactly how to do it. My programming language of choice is matlab. As far as I know there exists a function named randn and also a function named wgn (white gaussian noise). So please help me with this issue, to clarify, for example I want to generate following equation:
x(t)=20*sin(2*pi*f1*t)+30*cos(2*pi*f2*t)+A3*white noise
where A3=amplitude and white noise is in the range [-1 1]. Please help me and clarify how to do it. My confusion is related to white noise, not about the others, let's assume that t is changing from 1 to 100.

white noise is a random signal with a flat (constant) power spectral density. for that you can use rand. In order to obtain white noise in the interval [-1 1] you can just add to your expression white_noise=(rand(1,t)*2-1) .

I am not quite sure, but as natan says, you should be able to generate white noise from a uniform distribution of random samples.
I would proceed as follows:
wn = unifrnd(-seed,seed,[m,n])/seed;

Related

How to assign vector elements to get a square wave? [duplicate]

This question already has answers here:
How to have square wave in Matlab symbolic equation
(3 answers)
Closed 6 years ago.
I have a vector T that is defined as
T=zeros(1,4)
I want to define T such that T(1) and T(2) are equal to 1 and T(3) and T(4) are equal to 0. So that when I plot T it looks like a square wave.
I have tried
for i=1:2:size(T,2)
T(i:i+1)=1
end
figure; plot(T);
But this does not give the desired result. It turns out to be [1,0,1,0].
What is the right way to do this assignment?
To differentiate from questions about plotting square waves:
I wanted to find out how exactly to create the loop that would plot to look like a square wave, without explicitly defining frequency or using the symbolic equation. I would then use this information to modify another script that would do the same thing but a larger vector T where the "period" is not the same. Sometimes it is 11s, sometimes 9s and so on.
The period is 4, not 2:
for i=1:4:size(T,2)
T(i:i+1)=1
end
figure; plot(T);
If you have access to the signal processing toolbox, an alternative is using the square function:
T = (1+square(0:pi/2:3*pi/2))/2 %// 1 1 0 0

Gaussian derivative - Matlab

I have an RGB image and I am trying to calculate its Gaussian derivative.
Image is a greyscale image and the Gaussian window is 5x5,st is the standard deviation
This is the code i am using in order to find a 2D Gaussian derivative,in Matlab:
N=2
[X,Y]=meshgrid(-N:N,-N:N)
G=exp(-(x.^2+y.^2)/(2*st^2))/(2*pi*st)
G_x = -x.*G/(st^2);
G_x_s = G_x/sum(G_x(:));
G_y = -y.*G/(st^2);
G_y_s = G_y/sum(G_y(:));
where st is the standard deviation i am using. Before I proceed to the convolution of the Image using G_x_s and G_y_s, i have the following problem. When I use a standard deviation that is an even number(2,4,6,8) the program works and gives results as expected. But when i use an odd number for standard deviation (3 or 5) then the G_y_s value becomes Inf because sum(G_y(:))=0. I do not understand that behavior and I was wondering if there is some problem with the code or if in the above formula the standard deviation can only be an even number. Any help will be greatly appreciated.
Thank you.
Your program doesn't work at all. The results you find when using an even number is just because of some numerical errors.
Your G will be a matrix symmetrical to the center. x and y are both point symmetrical to the center. So the multiplication (G times x or y) will result in a matrix with a sum of zero. So a division by that sum is a division by zero. Everything else you observe is because of some roundoff errors. Here, I see a sum og G_xof about 1.3e-17.
I think your error is in the multiplication x.*G and y.*G. I can not figure out why you would do that.
I assume you want to do edge detection rigth? You can use fspecialto create several edge filters. Laplace of gaussian for instance. You could also create two gaussian filters with different standard deviations and subtract them from another to get an edge filter.

Axis scale and labeling in MATLAB plot [duplicate]

This question already has answers here:
matlab multiple x axis one below another
(3 answers)
Closed 7 years ago.
I've an FFT plot with negative and positive frequencies on X axis. I have a peak at Synchronous Frequency say 1X (1 times of fundamental frequency) and another peak sub synchronous frequency 0.30X (0.30 times of fundamental frequency). Now I want both the scaling to be labeled on the axis. I used the following code.
%for setting 1X frequency ( Synchronous frequency)
set(gca,'XTick',-5000:2500:5000)
set(gca,'XTickLabel',{'-2X','-1X','0','1X','-2X'})
%for setting 0.30X ( Sub harmonic frequency)
set(gca,'XTick',-700:700:700)
set(gca,'XTickLabel',{'-0.3X','0','0.3X'})
But I see only the second scaling where as the first is not being plotted. Is there any other way to plot this with 2 absolute values. Please find reference image. Thanks in advance.
As you can notice in the figure. I've been able to label the first set of frequencies (1X,1 1/2X, etc). Now I also want the second set(-30X , 0.30X etc) as I indicated with arrow arrow.
i am not sure what are you looking for but maybe this will work for you
c=[-3:3 -3:0.3:3]; % spacing of 1 and 0.3
c=c.*2500; % 2500 is fundamental frequency
c=unique(sort(c));
for i=1:length(c)
str1{i}=[num2str(c(i)/2500,'%.1f'),'X'];
end
plot(1,1,'+'); % random
hold on;
set(gca,'XTick',c,'XTickLabel',str1);
xlim([min(c) max(c)]);
Maybe I'm missing something or have misunderstood your question, but why don't you lump it into one command? As mentioned in the comments, the last set of commands overwrite the first one.
set(gca,'XTick',[-5000 -2500 -700 0 700 2500 5000])
set(gca,'XTickLabel',{'-2X','-1X','-0.3X','0','0.3X','1X','-2X'})

Detect steps in a Piecewise constant signal

I have a piecewise constant signal shown below. I want to detect the location of step transition (Marked in red).
My current approach:
Smooth signal using moving average filter (http://www.mathworks.com/help/signal/examples/signal-smoothing.html)
Perform Discrete Wavelet transform to get discontinuities
Locate the discontinuities to get the location of step transition
I am currently implementing the last step of detecting the discontinuities. However, I cannot get the precise location and end with many false detection.
My question:
Is this the correct approach?
If yes, can someone shed some info/ algorithm to use for the last step?
Please suggest an alternate/ better approach.
Thanks
Convolve your signal with a 1st derivative of a Gaussian to find the step positions, similar to a Canny edge detection in 1-D. You can do that in a multi-scale approach, starting from a "large" sigma (say ~10 pixels) detect local maxima, then to a smaller sigma (~2 pixels) to converge on the right pixels where the steps are.
You can see an implementation of this approach here.
If your function is really piecewise constant, why not use just abs of diff compared to a threshold?
th = 0.1;
x_steps = x(abs(diff(y)) > th)
where x a vector with your x-axis values, y is your y-axis data, and th is a threshold.
Example:
>> x = [2 3 4 5 6 7 8 9];
>> y = [1 1 1 2 2 2 3 3];
>> th = 0.1;
>> x_steps = x(abs(diff(y)) > th)
x_steps =
4 7
Regarding your point 3: (Please suggest an alternate/ better approach)
I suggest to use a Potts "filter". This is a variational approach to get an accurate estimation of your piecewise constant signal (similar to the total variation minimization). It can be interpreted as adaptive median filtering. Given the Potts estimate u, the jump points are the points of non-zero gradient of u, that is, diff(u) ~= 0. (There are free Matlab implementations of the Potts filters on the web)
See also http://en.wikipedia.org/wiki/Step_detection
Total Variation Denoising can produce a piecewise constant signal. Then, as pointed out above, "abs of diff compared to a threshold" returns the position of the transitions.
There exist very efficient algorithms for TVDN that process millions of data points within milliseconds:
http://www.gipsa-lab.grenoble-inp.fr/~laurent.condat/download/condat_fast_tv.c
Here's an implementation of a variational approach with python and matlab interface that also uses TVDN:
https://github.com/qubit-ulm/ebs
I think, smoothing with a sharper lowpass filter should work better.
Try to use medfilt1() (a median filter) instead, since you have very concrete levels. If you know how long your plateau is, you can take half/quarter of the plateau length for example. Then you would get very sharp edges. The sharp edges should be detectable using a Haar wavelet or even just using simple differentiation.

Using intensity transformation from a given graphic in a matrix

Using Matlab, I have to transform the intensity of an image using this given graphic:
where s=T(r)
I absolutely have no idea about this transformation. It's for an homework, can someone help me at least to recognize the function?
This is just the equation of a line, y=ax+b...
in matlab notation:
r=linspace(0,30,1000); %this can be whatever r units are... r=0:0.1:100 etc.
T=2*(r-10).*(r>=10 & r<=20);
plot(r,T)
will yield the plot you just draw...
The solution was s = 2r - 20 on the interval of [10, 20] and 0 otherwise.
Then, using an existing image, it's possible to modify the intensity.