Generate a Rectangular Pulse in MATLAB - matlab

I need to create a rectangular pulse with width = 7 and a range (-T/2, T/2) where T 59 msec.
I wrote this code but I'm not sure if that's correct.
w = 7;
T = 59;
t = -T/2:1:T/2;
rect = rectpuls(t, w);
plot(t, rect);
This code generates a rectangular pulse but I'm not sure if it's right. Also, I'm not quite sure what the t = -T/2:1:T/2; means. I mean the range is from -29.5 to 29.5 with step 1. When I set this to 0.1 or 0.01 my pulse is better. Why does this affect my output?
Note that the second thing I have to do is to create a periodic sequence of clock pulses. I don't know if this affects the way I must implement my initial rectangular pulse.

When you increase the number of increments a numerical function (such as Matlab rectpuls) uses in its process of discretizing the continuous, you'll have as consequence that the accuracy of said function is going to improve, at the expense (in this case, negligible) of added computational cost. You are doing exactly this, when you discretize employing smaller time-steps, from 1 to 0.1 to 0.01.
To create a periodic sequence of identical rectangular pulses, you can call the function in a loop:
w = 7;
T = 59;
t = -T/2:1:T/2;
t_size = size(t);
N = 10;
rect = zeros(N, t_size(2));
interval = 20;
figure
plot(t, rectpuls(t, w));
xlim([-20 (N + 1)*interval]);
ylim([0 1.1]);
hold on
for i = 1:N
t = (-T/2 + i*interval): 1 :(T/2 + i*interval);
rect(i,:) = rectpuls(t - i * interval, w);
plot(t, rect(i,:));
hold on
end
The above should generate identical rectangular pulses every interval = 20 ms, over a time length of interval * (N + 1) = 220 ms.

Related

Inverse Fourier of a Discrete and Finite Sampling Window

I am trying to find the inverse Fourier of a discrete sampling window with N_f = 11 elements. I am expecting to see a periodic Sinc-like signal in the time-domain:
However, this is the output I get:
Why the number of observed oscillations in my output is more than the expected output?
N_f = 11; % Number of samples in the finite sampling window in Frequency domain
delta_f = 1;
f = [-N_f/2+1:delta_f:N_f/2];
wf = ones(1, N_f)/N_f; % W(f):finite sampling window in Frequency Domain
N_t =128;
wt = ifftshift(ifft(wf, N_t))*N_t; % w(t):Inverse Fourier of the window
I think one of the issues with your code is related to the location of the origin. You create an array wf with 11 ones (and normalized), then call ifft(wf,128). This pads the array with zeros to a size of 128, but the 11 ones are on the left side. You can see this by doing
fft(ifft(wf, N_t))
Thus, your window is shifted by 5 samples to the right, covering frequency bins 0 through 11, rather than -5 through 5 (or identically, 0 through 5 and 124 through 128).
The code below creates a signal with 128 samples, and fills the 11 frequency bins around the middle with 1/11. By calling ifftshift we rotate the signal such that the middle element is moved to the leftmost bin. Now bins 0 through 5 and 124 through 128 are non-zero. I then call ifft, and fftshift to move the 0 frequency back to the middle of the signal. Finally, I plot three repetitions of this signal.
N_f = 11; % Number of samples in the finite sampling window in Frequency domain
N_t = 128;
wf = zeros(1,N_t);
wf( N_t/2 - floor(N_f/2) + 1 : N_t/2 + floor(N_f/2) + 1 ) = 1 / N_f;
wt = fftshift(ifft(ifftshift(wf))) * N_t;
figure; plot(repmat(wt,1,3))
As you can see, the result is as you expected it. Note that the wt result of ifft is actually real-valued. The result of your inverse transform wasn't real-valued, you had to ignore the imaginary component to produce your plot. That's a sign that the input signal wasn't symmetric!
We can change N_f to be twice as large, yielding a result similar to yours, but with a purely real output:
N_f = 21;
N_t = 128;
wf = zeros(1,N_t);
wf( N_t/2 - floor(N_f/2) + 1 : N_t/2 + floor(N_f/2) + 1 ) = 1 / N_f;
wt = fftshift(ifft(ifftshift(wf))) * N_t;
figure; plot(repmat(wt,1,3))

csc (cosecant) periodic equation and unexpected results

I can plot 1 (co-secant) every second over 2 seconds and it looks fine (the top plot). But when I plot the frequency of 100 csc (co-secant) signals I would expect / want 100 co-secant signals that are just repeating the same signal much like plotting y=.8*sin(100*t) would do but that doesn't occur what am I doing wrong? See code and plot below.
clear, clc
fs=8000
len_of_sig=2; %length of signal in seconds
t=linspace(0,2*pi*len_of_sig,fs*len_of_sig);
y_a=0.01*csc(1*t);
y_a(y_a >=1) = 1; %used to limit amplitude
y_a(y_a <=-1) = -1;
y_b=0.01*csc(100*t);
y_b(y_b >=1) = 1;
y_b(y_b <=-1) = -1;
t2=t./(2*pi); %converts time in radians to seconds
subplot(2,1,1);plot(t2(1:end),y_a(1:end));
subplot(2,1,2);plot(t2(1:end),y_b(1:end));
Ps: I'm using octave 4.0 which is like matlab
What you're seeing in the second subplot is the result of reduced sampling per cycle. You are increasing the frequency of y_b relative to y_a by a factor of 100, but still using the same vector of time points t, meaning you have 1/100th the number of points per cycle in y_b. These fewer points per cycle fall at slightly different offsets to the discontinuities, giving you the pattern above.
You can fix this by using an upsampled time vector for y_b:
t_up = linspace(0, 2*pi*len_of_sig, 100*fs*len_of_sig);
y_b = 0.01*csc(100*t_up);
y_b(y_b >= 1) = 1;
y_b(y_b <= -1) = -1;
subplot(2, 1, 2); plot(t_up./(2*pi), y_b);

Generating a sine signal with time dependent frequency in Matlab

I want to generate a sine signal y(t) with a time dependent frequency f(t) in Matlab.
I've already tried to realise this using the sine function of Matlab:
h = 0.0001;
npoints = 150/h;
for i = 1:1:npoints
f(i) = 2 - 0.01*i*h;
y(i) = 0.5*sin(2*3.1415*f(i)*i*h)+0.5;
end
where the frequency is decreasing with time and h is the time step width.
My problem:
The signal y(t) doesn't look I expected it to look like. There appears a bump in the amplitude at a distinct time (have a look at the plot below).
Does anyone know why this happens and how to generate this sine signal correctly?
what about
y(i) = 0.5*real(exp(1i*2*pi*f(i)*i*h))+0.5;
You will get the plot below
If you just need a chirp signal starting from 2Hz down to 0.5Hz, the following should do the job
f_start = 2; % start frequency
f_end = 0.5; % end frequency
endtime = 150; % seconds
timestep = 0.0001;
times = timestep:timestep:endtime;
y = chirp(times,f_start,endtime,f_end);
and if you plot it you get
figure(2);plot(times,y);
You can achieve the same manually using below
f_start = 2; % start frequency
f_end = 0.5; % end frequency
timestep = 0.0001;
T = 150;
rate_of_change = (f_start - f_end)/T;
times = timestep:timestep:T;
y = sin(2*pi*(f_start*times - times.^2*rate_of_change/2));
It might be useful to read the following Wikipedia page on Chirp signal.
At 100 you have sin(2*pi*N), which is 0. Change f a little bit, say to 2.0123-... and it goes to the top.
As for the general probably unexpected shape, consider what function you are using in the end (= substitute f back in the formula). You see that you have something of the form y = ...sin(Ai-B*i^2)..., which has a minimum at 100.
The easiest solution here is to simply offset frequency a little more, and use something like f(i) = 3.1 - ..., which has a minimum outside of your considered range.
It looks like there isn't actually a "bump in frequency", but at the 100 value on the x-axis the entire signal is shifted by 180 degrees. Bear in mind that the amplitude still reaches 0 and does not become smaller (e.g. from 0.25 to 0.75)
This is because the i value becomes so high that the value of f(i) changes sign.
Another indicator of this is that the frequency starts to increase again after the shift instead of gradually becoming even lower.
Why do you start off with the value of 2 in f(i)?
Sorry for asking for clarification here, but I cannot post it as a comment.

How can I make my code run faster?

I'm currently working on a piece of code which blurs an image by taking a circular sample of pixels around each pixel, finding their mean and then applying that to the central pixel. It works, but it takes ages, especially with large images and large radii.
Can anyone give me some tips on how to speed this up?
function [] = immean(IMAGE, r)
%Find the maximum width, nx, and maximum height, ny, of the image - so that
%it can be used to end the for-loop at the appropriate positions.
[nx, ny] = size(IMAGE);
%Create a completely black image of the same size as the subject image,
%into which the appropriate pixel values can be fed.
average = uint8(zeros(size(IMAGE)));
%Loop through all the pixels of the image.
for x = 1:nx
for y = 1:ny
%This next code takes a square sample of pixels, with dimensions of
%r x r.
%First, set the boundaries of this square, from which the circular
%sample of pixels will be taken.
if x-r <= 0
startx = 1;
else
startx = x-r;
end
if x+r > nx
endx = nx;
else
endx = x+r;
end
if y-r <= 0
starty = 1;
else
starty = y-r;
end
if y+r > ny
endy = ny;
else
endy = y+r;
end
%Loop through this square sample and, if the pixel is within the
%range of the circle, add its intensity to the total.
total = 0;
pixelcount = 0;
for xp = startx : endx
for yp = starty : endy
if (x-xp)^2 + (y-yp)^2 <= r^2
total = total + uint32(IMAGE(xp, yp));
pixelcount = pixelcount + 1;
end
end
end
mean = total / pixelcount;
average(x,y) = mean;
end
end
imshow(average)
I've tried changing things like uint32 but that hasn't worked. Beyond that, I'm a bit of a beginner, so I'm not sure what the best tricks are in this situation. Thanks for your time.
Loops are extremely slow in MATLAB. As a rule, you should always vectorize your code where possible. This is one of those cases. Looping through every pixel is painfully slow.
MATLAB has a function imfilter which does basically what you want. Since you are just taking the average intensity, a simple filter function will do this quite well and very fast. You can define the filter coefficients as a matrix:
% Define a 2D Filter with radius r:
d = 2*r+1;
h = zeros(d);
% Now make it a "circular" filter (keeping it square would be much easier
% and probably look the same but whatever):
[x, y] = meshgrid(1:d,1:d);
distance = sqrt((x-(r+1)).^2 + (y-(r+1)).^2);
h(distance<=r) = 1;
h = h / sum(h(:))
% Now pump it into imfilter and youre done:
average = imfilter(uint32(IMAGE), h);
Also, there are a TON of MATLAB image processing tools, so search around a bit and you'll probably find helpful tools for whatever your working on, no need to reinvent the wheel. I don't have an image in front of me to test this one but let me know if it works.

Scale Space for solving Sum of Gaussians

I'm attempting to use scale space implementation to fit n Gaussian curves to peaks in a noisy time series digital signal (measuring voltage).
To test it I created the following sample sum of three gaussians with noise (0.2*rand, sorry no picture, i'm new here)
amp = [2; 0.9; 1.3];
mu = [19; 23; 28];
sigma = [4.8; 1.3; 2.5];
x = linspace(1,50,1000);
for n=1:3, y(n,:) = A(n)*exp(-(x-B(n)).^2./(2*C(n)^2)); end
noisysignal = y(1,:) + y(2,:) + y(3,:) + 0.2*rand(1,numel(x))
I found this article http://www.engineering.wright.edu/~agoshtas/GMIP94.pdf posted by user355856 answer to thread "Peak decomposition"!
I believe my code generates the correct result for plotting the zero crossings as a function of the gaussian filter resolution sigma, but I have two issues. The first is that it seems yet another fitting routine would be needed to identify the approximate location of the arch intercepts for approximating the initial peak sigma and mu values. The second is that the edges of the scale space plot have substantial arches that definitely do not correspond to any peak. I'm not sure how to screen these out effectively. Last thing is that is used a spacing of 50 when calculating the second derivative central finite difference since too much more destroyed feature, and to much less results in a forest of zero crossings. Would there be a better way to filter that to control random zero crossings in the gaussian peak tails?
function [crossing] = scalespace(x, y, sigmalimit)
figure; hold on; ylim([0 sigmalimit]);
for sigma = 1:sigmalimit %
yconv = convkernel(sigma, y); %convolve with kernel
xconv = linspace(x(1), x(end), length(yconv));
yconvpp = d2centralfinite(xconv, yconv, 50); % 50 was empirically chosen
num = 0;
for i = 1 : length(yconvpp)-1
if sign(yconvpp(i)) ~= sign(yconvpp(i+1))
crossing(sigma, num+1) = xconv(i);
num = num+1;
end
end
plot(crossing(sigma, crossing(sigma, :) ~= 0),...
sigma*ones(1, numel(crossing(sigma, crossing(sigma, :) ~= 0))), '.');
end
function [yconv] = convkernel(sigma, y)
t = sigma^2;
C = 3; % for kernel truncation
M = C*round(sqrt(t))+1;
window = (-M) : (+M);
G = zeros(1, length(window));
G(:) = (1/(2*pi()*t))*exp(-(window.^2)./(2*t));
yconv = conv(G, y);
This is my first post and I apologize in advance for any issues in style. I'm fairly new to programming, so any advice regarding the programming style or information provided in this question would be much appreciated. I also read through Amro's answer about matlab's GMM function! if anyone feels that would be a more efficient approach to modeling multiple gaussians in a digital signal.
Thank you!