I'm novice in matlab programing and I thank you in advance for your help.
I would like to generate un sinus wave function starting to 0.25 Hz and increasing every 8 oscillations by 0.05 Hz until 0.7 Hz. The amplitude is constant.
I try this code:
cyc = 8; % number of cycles
for lF= 0.25:0.05:0.7 %% Frequency Increment
t=1/fE:1/fE:(cyc/lF); % time per freq step
wave = sin(2*pi*t)
end
plot(wave)
Thank you for your help.
1) Try this:
cyc = 8; % number of cycles
wave = [];
T = [];
for f = 0.25:0.05:0.7 %% Frequency Increment
t=0.01/f:0.01/f:(cyc/f); % time per freq step
wave = [wave,sin(2*pi*f*t)];
if isempty(T)
T = t;
else
T = [T,T(end)+t];
end
end
plot(T,wave)
2) Following to comment, the code with memory preallocation:
cyc = 8; % number of cycles
Npoints = 100; % number of points in one cycle
f = 0.25:0.05:0.7; %% Frequency Increment
Nf = length(f);
wave = zeros((cyc*Npoints-1)*Nf+1,1);
T = zeros((cyc*Npoints-1)*Nf+1,1);
t0 = 0;
t_f = linspace(0,cyc,cyc*Npoints); % time*frequency per freq step
for nf = 1:length(f)
ind = (nf-1)*(cyc*Npoints-1)+(1:cyc*Npoints);
wave(ind) = sin(2*pi*f(nf)*t_f);
T(ind) = t0 + t/f(nf);
t0 = t0+cyc/f(nf);
end
plot(T,wave)
3) Vectorized version:
t = cyc*linspace(0,1,cyc*Npoints)';
wave = sin(2*pi*t(:,ones(Nf,1)));
t = bsxfun(#plus,cumsum([0,cyc./f(1:end-1)]),bsxfun(#times,1./f,t))'; % for MATLAB version less than 2016b
[t,ind] = unique(t); wave = wave(ind); % remove duplicate values
plot(t,wave)
Related
I have written a simple code to calculate the phase and magnitude of a signal, based on the sinusoidal input given in my problem. I have already determined the magnitude of the signal corresponding to different values of w. More specifically, the phase I want is a vector calculated for different values of w. Notice that the signal I'm talking about is the output signal of a linear process. As matter of fact, I want the phase difference between the input u and the output y, defined for all values of w for all time steps. I have created the time and w vector in my code. Here is the main code I have written in MATAB2021a:
clc;clear;close all;
%{
Problem 2 Simulation, By M.Sajjadi
%}
%% Predifined Parameters
tMin = 0;
tMax = 50;
Ts = 0.01; % Sample Time
n = tMax/Ts; % #Number of iterations
t = linspace(tMin,tMax,n);
% Hint: Frequency Domain
wMin = 10^-pi;
wMax = 10^pi;
Tw = 10;
w = wMin:Tw:wMax; % Vector of Frequency
Nw = length(w);
a1 = 1.8;
a2 = -0.95;
a3 = 0.13;
b1 = 1.3;
b2 = -0.5;
%% Input Generation
M = numel(w);
N = length(t);
U = zeros(M,N);
Y = U; % Response to the sinusoidal Input, Which means the initial conditions are set to ZERO.
U(1,:) = sin(w(1)*t);
U(2,:) = sin(w(2)*t);
U(3,:) = sin(w(3)*t);
Order = 3; % The Order of the Differential Equation, Delay.
%% Main Loop for Amplitude and Phase
Amplitude = zeros(Nw,1); % Amplitude Values
for i=1:numel(w)
U(i,:) = sin(w(i)*t);
for j=Order+1:numel(t)
Y(i,j) = a1*Y(i,j-1) + a2*Y(i,j-2) + a3*Y(i,j-3) + ...
b1*U(i,j-1) + b2*U(i,j-2);
end
Amplitude(i) = max(abs(Y(i,:)));
end
I know I should use fft or findpeaks function in MATLAB, but I do not know how I should do it.
I have a half wave sin:
Rc = 1e2; % [b/s] chip rate
T =1; % [s/b] inverse of chip rate
Fs = 2e2; % [Hz] sampling frequency
dt = 1/Fs; % [s]
sps = 40;
dt2=dt/sps;
T = 1;
% single pulse time reference
t = 0:dt2:2*T;
pulse_half_Sine = sin(pi*t/(2*T)).^3;
I have a modulated signal
N=1e4;
bits=randi([0,1],N,1);
modOrder = 2;
modData = pammod(bits,modOrder);
I want to do a convolution of the signal and the pulse. The duration of the envelope is T and the symbolic speed = 2/T. Thus, the maximum should be at the point T/2. I do so, but I don't get the countdown points of time.
over_data=upsample(modData,length(pulse_half_Sine)-1);
plot(t, pulse_half_Sine);
signal1 = conv(over_data,pulse_half_Sine);
As part of the assignment, we have to plot the free vibration response of a single degree of freedom graph over the displacement vs time. The t should increase up to 3 seconds with the increment of the .05. When I try to plot the graph based on the code below I am not getting any graphs at all. Please any help would be highly appreciated. Please let me know if need to provide more information.
% Main subroutine to generate the response of a SDOF
z = 0.05;
w = 2*pi;
dt = 0.05; % dt represent the change in time which is constant
Tmax = 3;
TN = 1.0;
i = 0; % number of time step starting with 0
x = 0; % initial position
v = 0; % velocity at start time and position
x_vector = [x; v];
t = [0:0.05:3]; % defining the time vector 0 to 3 seconds
t_i = t;
% Insert the DHMAT scripts for calculating the matrix A and B
w_d = w*sqrt(1-z.^2);
DHMAT;
% Start the loop
for t_i = [0:dt:Tmax]
% Insert the EXCIT script to calculate the excitation (f_vector)
f = EXIT(t, dt);
f_vector =[1;1];
i = i+1;
x_vectornew = A*x_vector - B*f_vector;
% t_i = t_i + dt;
end
plot(t_i, x_vectornew);
I want to plot multiple realizations of a stochastic process in matlab. For a single realization I have the following code:
N = 80;
T = dt*N;
dWt = zeros(1,N);
S= repmat(S0,1,N);
S(1) = S0;
dWt = sqrt(dt) * randn;
for t=2:N
dWt(t) = sqrt(dt)*randn;
dSt = k*(mu-S(t-1))*dt + sigma*S(t-1)*dWt(t);
S(t) = S(t-1)+dSt;
end
plot(handles.pMeasure, [0:dt:T],[S0,S]);
I want to replicate this loop n times and plot the results in one plot.
You could add an additional for loop, but it would be best to vectorize everything and calculate all n instances at once:
k = ...
mu = ...
sigma = ...
S0 = ... % Initial condition
dt = ... % Time step
n = ... % Number of instances
N = 80; % Number of time steps, not counting initial condition
T = dt*N; % Final time
rng(1); % Always seed random number generator
dWt = sigma*sqrt(dt)*randn(n,N); % Calculate Wiener increments
S = zeros(n,N+1); % Allocate
S(:,1) = S0; % Set initial conditions
for t = 2:N+1
S(:,t) = S(:,t-1) + k*(mu-S(:,t-1))*dt + S(:,t-1).*dWt(:,t-1);
end
plot(handles.pMeasure,0:dt:T,S)
There are further ways to optimize this if want or you can also try sde_euler in my SDETools Matlab toolbox:
k = ...
mu = ...
sigma = ...
dt = ... % Time step
n = ... % Number of instances
N = 80; % Number of time steps, not counting initial condition
T = dt*N; % Final time
f = #(t,y)k*(mu-y); % Diffusion function
g = #(t,y)sigma*y; % Drift function
t = 0:dt:T; % Time vector
S0 = zeros(n,1); % Initial conditions
opts = sdeset('RandSeed',1,...
'SDEType','Ito'); % Set random seed, specify Ito SDE
S = sde_euler(f,g,t,S0,opts); % Simulate
plot(t,S)
I have go a clean signal (manchester coding), and the same signal with an including noise - what formula I have to use to get the Signal to Noise Ratio?
%manchester code
T = length(bits)/bitrate; % full time of bit sequence
n = 200;
N = n*length(bits);
dt = T/N;
t = 0:dt:T;
x = zeros(1,length(t)); % output signal
for i = 0:length(bits)-1
if bits(i+1) == 1
x(i*n+1:(i+0.5)*n) = 1;
x((i+0.5)*n+1:(i+1)*n) = -1;
else
x(i*n+1:(i+0.5)*n) = -1;
x((i+0.5)*n+1:(i+1)*n) = 1;
end
end
It is relatively easy to do this in Matlab and there are several approaches. Good to mention is, that there is the snr-function in the Signal Processing Toolbox. If you don't have it, then use the RMS-values like described here.
An alternative for zero-mean signals is to use an approach with the standard deviation or the variance. Important: They do only work when the mean (expected value) is zero.
Here the code:
% random data
bits = randi(2,1,20)-1;
bitrate = 1;
% manchester code
T = length(bits)/bitrate; % full time of bit sequence
n = 200;
N = n*length(bits);
dt = T/N;
t = 0:dt:T;
x = zeros(1,length(t)); % output signal
for i = 0:length(bits)-1
if bits(i+1) == 1
x(i*n+1:(i+0.5)*n) = 1;
x((i+0.5)*n+1:(i+1)*n) = -1;
else
x(i*n+1:(i+0.5)*n) = -1;
x((i+0.5)*n+1:(i+1)*n) = 1;
end
end
% add offset to test (approach with standard deviation
% and variance are NOT going to produce the correct SNR then)
%x = x + 1;
% x is the clean signal
s = x + 0.1*randn(size(x)); % noisy signal
e = s - x; % noise
% Matlab function in the Signal Processing Toolbox
SNR = snr(x,e)
% with RMS values
rms_x = sqrt(mean(x.^2));
rms_e = sqrt(mean(e.^2));
SNR = 10*log10((rms_x/rms_e)^2) % SNR in dB
SNR = 20*log10(rms_x/rms_e) % SNR in dB
% with standard deviation (only for signals with zero-mean)
sigma_x = std(x);
sigma_e = std(e);
SNR = 20*log10(sigma_x/sigma_e) % SNR in dB
% with variance (only for signals with zero-mean)
var_x = var(x);
var_e = var(e);
SNR = 10*log10(var_x/var_e) % SNR in dB
This is the result:
SNR =
20.1780
SNR =
20.1780
SNR =
20.1780
SNR =
20.1781
SNR =
20.1781