Can the amplitude of the step function in Scipy Signal be changed?
The amplitude in the step function has a default value of 1. Can the amplitude be increased or decreased?
sys = ct.tf([10], [20, 1])
# Generate step response
(y, T) = ct.step(sys)
plt.plot(T, y)
plt.show()
Related
I have been trying to write down a code for an LTI system where the response is calculated from an input x(t) and impulse h(t) response.
For the part of the code below:
y = conv(x,h).*steps;
ty = 0:steps:7;
plot(ty,y);
I'm getting the following error message:
Error using plot
Vectors must be the same length.
I'm using ty = 0:steps:7; as h(t) is defined as exp(-t/2).*((t>=2)-(t>=7)) (since it extends upto t=7).
What does actually decide ty?
Convolution Using Anonymous Functions
One way to do this process is to use anonymous functions/functions handles which are indicated by the #() that holds the input parameters in this case time, t. To create truncated signals conditional statements on t can be implemented. To have a signal ranging from t=2 to t=7 seconds truncation can be done by element-wise multiplying by ((t>=2) & (t<=7)). The t vector used to plot the final result must have a time range that is the sum of the lengths of time of the signals used in the convolution process. I believe ty is the time vector to plot the output against. In this case you must ensure ty has the same length as the output y.
Length of Results = Length of System Response + Length of Input Signal
In the case below:
x(t) → length = 1s
h(t) → length = 5s (2s to 7s)
y(t) → length = 1s + 5s = 6s (2s to 8s)
Step_Size = 0.1;
Start_Time = 0; End_Time = 7;
t = Start_Time: Step_Size: End_Time;
%Input into system x(t)%
x = #(t) 1.0.*(t >= 0 & t <= 1);
subplot(3,1,2); fplot(x);
title('x(t): Input into system');
xlabel('Time (s)'); ylabel('Amplitude');
xlim([0 10]);
ylim([0 1.1]);
%System impulse response h(t)%
h = #(t) exp(-t/2).*((t>=2) & (t<=7));
subplot(3,1,1); fplot(h);
title('h(t): System impulse response');
xlabel('Time (s)'); ylabel('Amplitude');
xlim([0 10]);
y = conv(x(t),h(t)).*Step_Size;
t = linspace(0,2*max(t),length(y));
subplot(3,1,3); plot(t,y);
title('y(t): Output/System Response');
xlabel('Time (s)'); ylabel('Amplitude');
I want to upsample by 5 a signal in frequency domain, and then plot (stem) it. I figured how to upsample,
Fk=(1/5)*upsample(ak_new,5);
now this creates a vector that is 5 times bigger than the original one, and I need to take the inverse Fourier series of this signal
Fn=(Fk*(exp((1i*2*pi/N*n'*n))));
where n is a sample vector (-1000:1000), as you can see, I can't make the transformation since n is not the same size as Fk anymore. How can I solve this?
You need to plot your upsampled signal in frequency domain, on a similarly "upsampled" frequency vector.
If your initial frequency vector was -1000, -999, -998 ..., now it should be -1000, -999.8, -999.6.
Here is a simple example:
Fs = 2000; % Sampling frequency
T = 1/Fs; % Sampling period
L = 2000; % Length of signal
t = (0:L-1)*T; % Time vector
S = sin(2*pi*400*t); % Signal
Y = fft(S);
ak_new = fftshift(abs(Y/L)); % Initial signal in frequency domain
Fk = upsample(ak_new,5); % Upsampled signal
f = (Fs/L)*(-L/2: 1 :L/2-1); % Initial frequency vector
fup = (Fs/L)*(-L/2:(1/5):L/2-1/5); % Upsampled frequency vector
subplot(2, 1, 1);
stem(f, ak_new);
title('Before upsampling');
subplot(2, 1, 2);
stem(fup, Fk);
title('After upsampling');
I am trying to find the inverse Fourier transform of a simple filter in Matlab. In the first case (sinc filter / "brick wall"), I use the ifft function to find the time-domain function, which is a sinc, centered at t = 0.
I now want to now find the time-domain function for a simple Chebyshev filter. However, for some reason, the code below seems to give me the impulse response, where the time axis is incorrect. Should I not expect a similar looking sinc function centered at t = 0?
fo = 10; %frequency of the sine wave
Fs = 100; %sampling rate
Ts = 1/Fs; %sampling time interval
t = -1+Ts:Ts:1-Ts; %sampling period
freq = -Fs/2:(Fs/length(t)):Fs/2;
%% Sinc with bandwidth = fo. This works!
y = 0.5*sinc(2*fo*t);
YfreqDomain1 = fft(y);
figure('Name','Brick wall sinc filter (freq)');
plot(freq(1:length(y)),2/length(y)*fftshift(abs(YfreqDomain1)))
y_ret1=ifft(YfreqDomain1,'nonsymmetric');
figure('Name','Brick wall sinc filter (time)');
plot(t,y_ret1);
%% Chebyshev with bandwidth fo. This gives me a strange result.
[b,a] = cheby1(6,0.1,2*fo/Fs); % 6th order, 0.1dB ripple
[YfreqDomain2 w] = freqz(b,a,length(t),'whole');
figure('Name','Chebyshev Filter (freq)');
plot(freq(1:length(YfreqDomain2)), 2/length(y)*fftshift(abs(YfreqDomain2)));
figure('Name','Chebyshev Filter (time)');
y_ret2=ifft(YfreqDomain2,'nonsymmetric');
plot(t,y_ret2);
There are two problems with your computation:
First, you evaluate the time domain filter coefficients on a very narrow time window, which truncates the filter and changes its characteristics.
Second, you do not properly keep track of which indices in the time domain and frequency domain vectors correspond to time point 0 and frequency 0, respectively. This can be done differently, I here chose to always have t(1) = 0 and f(1) = 0, and apply fftshift for plotting only.
Here's my corrected version of your code:
fo = 10; % frequency of the sine wave
Fs = 100; % sampling rate
Ts = 1 / Fs; % sampling time interval
n = 1000; % number of samples
% prepare sampling time vector such that t(1) = 0
t = (0 : n - 1)';
t = t - n * (t >= 0.5 * n);
t = t / Fs;
% prepare frequency vector such that f(1) = 0;
f = (0 : n - 1)' / n;
f = f - (f >= 0.5);
f = f * Fs;
%% sinc filter with bandwidth fo
% define filter in time domain
s = 2*fo/Fs * sinc(2*fo*t);
% transform into frequency domain
Hs = fft(s);
% since the filter is symmetric in time, it is purely real in the frequency
% domain. remove numeric deviations from that:
Hs = real(Hs);
subplot(2, 4, 1)
plot(fftshift(t), fftshift(s))
ylim([-0.1 0.25])
title('sinc, time domain')
subplot(2, 4, 2)
plot(fftshift(f), real(fftshift(Hs)), ...
fftshift(f), imag(fftshift(Hs)))
ylim([-1.1 1.1])
title({'sinc, frequency domain', 'real / imaginary'})
subplot(2, 4, 3)
plot(fftshift(f), abs(fftshift(Hs)))
ylim([-0.1 1.1])
title({'sinc, frequency domain', 'modulus'})
%% Chebyshev filter with bandwidth fo
% define filter in frequency domain
[b,a] = cheby1(6, 0.1, 2*fo/Fs); % 6th order, 0.1 dB ripple
Hc = freqz(b, a, n, 'whole', Fs);
% transform into time domain
c = ifft(Hc);
% determine phase such that phase(1) is as close to 0 as possible
phase = fftshift(unwrap(angle(fftshift(Hc))));
phase = phase - 2*pi * round(phase(1) /2/pi);
subplot(2, 4, 5)
plot(fftshift(t), fftshift(c))
title('Chebyshev, time domain')
ylim([-0.1 0.25])
subplot(2, 4, 6)
plot(fftshift(f), real(fftshift(Hc)), ...
fftshift(f), imag(fftshift(Hc)))
ylim([-1.1 1.1])
title({'Chebyshev, frequency domain', 'real / imaginary'})
subplot(2, 4, 7)
plot(fftshift(f), abs(fftshift(Hc)))
ylim([-0.1 1.1])
title({'Chebyshev, frequency domain', 'modulus'})
subplot(2, 4, 8)
plot(fftshift(f), fftshift(phase))
title({'Chebyshev, frequency domain', 'phase'})
And here's the result:
As you can see, the sinc and Chebyshev filters are similar with respect to the modulus of the frequency response, but very different regarding the phase. The reason is that the Chebyshev filter is a causal filter, meaning that the coefficients in the time domain are constrained to be 0 for t < 0, a natural property for a filter that is implemented in a real physical system.
I tried this as well:
plot(x(bootsam(:,100)),y(bootsam(:,100)), 'r*') but it was exactly the same to my data! I want to resample my data in 95% confidence interval .
But it seems this command bootstrp doesn't work alone, it needs some function or other commands to combine. Would you help me to figure it out?
I would like to generate some data randomly but behave like my function around the original data, I attached a plot which original data which are red and resampled data are in blue and green colors.
Generally, I would like to use bootstrap to find error for my best-fit parameters. I read in this book:
http://books.google.de/books?id=ekyupqnDFzMC&lpg=PA131&vq=bootstrap&hl=de&pg=PA130#v=onepage&q&f=false
other methods for error analysis my fitted parameters are appreciated.
I suggest you start this way and then adapt it to your case.
% One step at a time.
% Step 1: Suppose you generate a simple linear deterministic trend with
% noise from the standardized Gaussian distribution:
N = 1000; % number of points
x = [(1:N)', ones(N, 1)]; % x values
b = [0.15, 157]'; % parameters
y = x * b + 10 * randn(N, 1); % linear trend with noise
% Step 2: Suppose you want to fit y with a linear equation:
[b_hat, bint1] = regress(y, x); % estimate parameters with linear regression
y_fit = x * b_hat; % calculate fitted values
resid = y - y_fit; % calculate residuals
plot(x(:, 1), y, '.') % plot
hold on
plot(x(:, 1), y_fit, 'r', 'LineWidth', 5) % fitted values
% Step 3: use bootstrap approach to estimate the confidence interval of
% regression parameters
N_boot = 10000; % size of bootstrap
b_boot = bootstrp(N_boot, #(bootr)regress(y_fit + bootr, x), resid); % bootstrap
bint2 = prctile(b_boot, [2.5, 97.5])'; % percentiles 2.5 and 97.5, a 95% confidence interval
% The confidence intervals obtained with regress and bootstrp are
% practically identical:
bint1
bint2
At the plot of the code that written below, i should see amplitude between values of 75 to 200 uV but there is something strange in my plot, can you help me and advise, what wrong with my code and how can i correct that ?
Thank you all
Here is the code :
clear all;
close all;
Fs = 200
t= 0:1/Fs:180
y_in=zeros (1, length(t));
for i = 1:18
F = randi ([4 7], 1);% frequency
A = randi ([75 200],1);% amplitude A=75~200 uV.
y_tmp =A*sin (2*pi*F*t);
y_in=y_in+ y_tmp;
end;
L=length (y_in);
NFFT = 2^nextpow2(L); % Next power of 2 from length of y
Y_new = fft(y_in,NFFT)/L;
f_new = Fs/2*linspace(0,1,NFFT/2+1);
figure(1)
plot(f_new,2*abs(Y_new(1:NFFT/2+1))) ;
title('Single-Sided Amplitude Spectrum of y_in(t)')
xlabel('Frequency (Hz)')
ylabel('|y_in(f)|')
Since you're looping 18 times and use
F = randi([4 7],1);
some of the frequencies that build up the final signal occurs multiple times, hence their amplitudes are added to each other.
What you're seeing after the fft in your plot is therefore the sum of the amplitudes for the frequencies
F = 4, 5, 6, 7 Hz