I'm trying to create some sin(2x) 2000HZ, square wave 1000hz, triangle wave 1000 hz, sawtooth 1000 hz. Number of points per each graph should be 62000.
Is this a good start? For some reason y2,y3,y4 are not created..
t = 0:0.001:0.62; % Sampling frequency 6.2kHz
y1=sin(2*pi*2000*t);
y2 =square(2*pi*1000*t);
y3= sawtooth(2*pi*1000*t);
y4= sawtooth(2*pi*1000*t,1/2); %triangle
Update:
t = 0:0.001:0.62; % Sampling frequency 6.2kHz
y1 = sin(2000*t);
y2 = square(1000*t);
y3 = sawtooth(1000*t);
y4= 10 * sawtooth(1000*t ,0.5) + 5;
Square and sawtooth functions require the Signal Processing Toolbox.
So you can also create your own function:
t = 0:0.1:8*pi;
y1 = sin(t);
y2 = square(t);
y3 = sawtooth(t);
With square.m:
function y = square(x)
inp = sin(x) >= 0;
y(~inp) = -1;
y(inp) = 1;
end
With sawtooth.m
function y = sawtooth(x)
y = ((mod(x,2*pi)/(pi*2))*2)-1;
end
Result:
square and sawtooth functions require the Signal Processing Toolbox.
The two first lines generate a sinusoidal signal correctly.
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.
How to rotate the following signal around the x-axis using MATLAB.Let's say y is located in the plane xy. I want to move it to yz plane. Thank you
duration = 3; % seconds
samplingRate = 8192; % per second
samplingInterval = 1 / samplingRate;
t = 1:samplingInterval:duration;
y1 = cos(2*pi*300*t); % signal
y2 = cos(2*pi*5*t); % modulating signal (frequency = 5 Hz, adjust as desired)
y2 = rescale(y2,0.25,1); % modulation amplitude min/max factor
y = y1 .* y2; % create amplitude-modulated signal
% plot about 1 second of the signal (to show amplitude modulation)
plot(y(1:9000))
set(gca,'ylim', [-1.2, 1.2]);
How can I get the correct frequency vector to plot using the FFT of MATLAB?
My problem:
N = 64;
n = 0:N-1;
phi1 = 2*(rand-0.5)*pi;
omega1 = pi/6;
phi2 = 2*(rand-0.5)*pi;
omega2 = 5*pi/6;
w = randn(1,N); % noise
x = 2*exp(1i*(n*omega1+phi1))+4*sin(n*omega2+phi2);
h = rectwin(N).';
x = x.*h;
X = abs(fft(x));
Normally I'd do this :
f = f = Fs/Nsamples*(0:Nsamples/2-1); % Prepare freq data for plot
The problem is this time I do not have a Fs (sample frequency).
How can I do it correctly in this case?
If you don't have a Fs, simply set it to 1 (as in one sample per sample). This is the typical solution I've always used and seen everybody else use. Your frequencies will run from 0 to 1 (or -0.5 to 0.5), without units. This will be recognized by everyone as meaning "periods per sample".
Edit
From your comment I conclude that you are interested in radial frequencies. In that case you want to set your plot x-axis to
omega = 2*pi*f;
In the below code I am trying to get the fourier transform of a stationary signal (x3). But at run time the plot i get is absolutely something wrong and does not show any frequencies of the signals x3.
kindly please guide me and help me to get the fourier transform correctly.
Code:
%% Time specifications:
Fs = 8000; % samples per second
dt = 1/Fs; % seconds per sample
StopTime = 1; % seconds
t = (0:dt:StopTime-dt); % seconds
x1 = (10)*cos(2*pi*3*(t));
x2 = x1 + (10)*cos(2*pi*5*(t));
x3 = x2 + (10)*cos(2*pi*10*(t));
%% here i try to Plot fourier transform of the signal x3:
NFFT = 2^nextpow2(StopTime); % Next power of 2 from length of y
Y = fft(y,NFFT)/StopTime;
f = Fs/2*linspace(0,1,NFFT/2+1);
figure;
plot(f,2*abs(Y(1:NFFT/2+1)));
%% Plot the signal versus time:
figure;
hold on;
plot(t,x1,'r');
plot(t,x2,'g');
plot(t,x3,'b');
Update_1
You can not see what you expected because the value of NFFT is 1 means when you write NFFT/2+1 as an index of Y it will not be an integer value so MATLAB warns you. You can calculate NFFT like this:
NFFT = 2^nextpow2(length(t))
instead of writing
NFFT = 2^nextpow2(StopTime)
Well, try this:
Fs = 8000; % samples per second
dt = 1/Fs; % seconds per sample
StopTime = 1; % seconds
t = (0 : dt : StopTime-dt); % seconds
x1 = 10 * cos(2*pi*3*t);
x2 = x1 + 10 * cos(2*pi*5*t);
x3 = x2 + 10 * cos(2*pi*10*t);
%% here i try to Plot fourier transform of the signal x3:
NFFT = 2 ^ nextpow2(length(t)); % Next power of 2 from length of y
Y = fft(x3, NFFT) / StopTime;
f = Fs / 2 * linspace(0, 1, NFFT/2 + 1);
figure;
plot(f, 2 * abs( Y( 1:length(f) ) ) ); % // Also try this: plot(f(f <= 200), 2 * abs( Y( 1:length(f(f <= 200)) ) ) )
%% Plot the signal versus time:
figure;
hold on;
plot(t, x1, 'r');
plot(t, x2, 'g');
plot(t, x3, 'b');
Plots:
EDIT:
1- Actually you don' t have to use nextpow() function. If you use it, fft() function works faster. Because, due to time efficiency, fft() works like that recursively divide the signal by 2 for each time. Then calculates discrete fourier transform for each part and gather them. This means that the FFT is most efficient when the signal vector length is a power of 2.
2- Dividing fft result by StopTime part doesn' t make any sense to me either. Dividing fft result by NFFT may be more convenient theoretically.
I have four frequency components of non stationary signals defined as shown below in the code. When i tried to plot the frequency domain of these signals, i got a graph with only three frequency peaks as shown below in the image.?!
kindly let me know why ia m getting only three peaks while i have four freq. components.
COde:
% Time specifications:
Fs = 8000; % samples per second
dt = 1/Fs; % seconds per sample
StopTime = 2; % seconds
t = (0:dt:StopTime-dt); % seconds
t1 = (0:dt:.25);
t2 = (.25:dt:.50);
t3 = (.5:dt:.75);
t4 = (.75:dt:1);
x1 = (10)*sin(2*pi*10*t1);
x2 = (10)*sin(2*pi*20*t2) + x1;
x3 = (10)*sin(2*pi*50*t3) + x2;
x4 = (10)*sin(2*pi*70*t4) + x3;
NFFT = 2 ^ nextpow2(length(t)); % Next power of 2 from length of y
Y = fft(x4, NFFT);
f = Fs / 2 * linspace(0, 1, NFFT/2 + 1);
figure;
plot(f(1:200), 2 * abs( Y( 1:200) ) );
% Plot the signal versus time:
figure;
xlabel('time (in seconds)');
ylabel('Amplitude');
title('non-stationary Signal versus Time');
hold on
plot(t1,x1,'r');
plot(t2,x2,'g');
plot(t3,x3,'b');
plot(t4,x4,'black');
legend('x1 = (10)*sin(2*pi*15*t1) + (10)*sin(2*pi*8*t1)', 'x2 = (10)*sin(2*pi*25*t2)
+
x1', 'x3 = (10)*sin(2*pi*50*t3) + x2', 'x4 = (10)*sin(2*pi*75*t4) + x3', ...
'Location', 'SouthWest');
IMage::
You've plotted the fft for x3, which is the sum of the first three signals only. I think you meant to plot this for x4, which includes the fourth signal.