Matlab:Interpolation Error - matlab

I would like to perform a frame based analysis on the following curve Which expresses the relation between time and concentration (x-axis: time in minutes; y-axis: concentration in Mbq):
For the above curve I would like to perform frame based sampling by splitting the curve into 19 frames:
19 frames:
4 frames : Each of 15 seconds time interval
2 frames : Each of 30 seconds time interval
2 frames : Each of 60 seconds time interval
11 frames : Each of 200 seconds time interval
I have written the following interpolation function for the frame analysis. c_t is where my signal which was expressed in the figure above is stored:
function c_i = Frame_analysis(a1,a2,a3,b1,b2,b3,td,tmax,k1,k2,k3)
t= 0:6:3000; % The original sample time, in seconds
t_i =[0:15:60,90:30:120,180:60:240,440:200:2240];% The interpolated sample time for first 4 frames of 15 second interval
K_1 = (k1*k2)/(k2+k3);
K_2 = (k1*k3)/(k2+k3);
%DV_free= k1/(k2+k3);
c_t = zeros(size(t));
ind = (t > td) & (t < tmax);
c_t(ind)= conv(((t(ind) - td) ./ (tmax - td) * (a1 + a2 + a3)),(K_1*exp(-(k2+k3)*t(ind)+K_2)),'same');
% Y_i = interp1(t,c_t(ind), t_i); % Interpolation for first frame
ind = (t >= tmax);
c_t(ind)=conv((a1 * exp(-b1 * (t(ind) - tmax))+ a2 * exp(-b2 * (t(ind) - tmax))) + a3 * exp(-b3 * (t(ind) - tmax)),(K_1*exp(-(k2+k3)*t(ind)+K_2)),'same');
c_i = interp1(c_t(ind),t_i);% Interpolation for Next consequtive frames
figure;
plot(t_i,c_i);
axis([0 3000 -2000 80000]);
xlabel('Time[secs]');
ylabel('concentration [Mbq]');
title('My signal');
%plot(t,c_tnp);
end
When I run the code, I have obtained a curve without any interpolation as you can see from figure expressed below:
Where have I made a mistake in my code and how can I possibly perform a better interpolation for obtaining different frames in my Output curve expressed in first figure?
Following are the input values which i have provided manually
Frame_analysis(2501,18500,65000,0.5,0.7,0.3,3,8,0.014,0.051,0.07)

There are multiple issues here, and it will be up to you to decide how to address them.
This is what I can provide:
You do not specify an x-coordinate for your plot command. That can be done, however, Matlab will use the index of the vector for the x-axis. Example: if you have
y = 1:2:6;
plot(y);
hold on;
y = 1:1:6;
plot(y);
you'll see the difference.
How does this apply to your case? You specify a vector of higher resolution (t_i, compared to t), but in your plot command you do not provide this new vector for the x-coordinate.
Your definition of c_t provides very very small values (on the order of 10^-77). And from t > 60, your output is indifferent from 0.
How does that effect your interpolation?
You specify that for the interval [0 60] you want step-sizes of 15 - that does not give you a lot of resolution:
You might want to change to something like:
t_i =[0:0.5:60,90:30:120,180:60:240,440:200:2240];
Which will give you this plot:
In either case, I do not understand why you chose a data range above 60 (all the way until 3000) for the data you are trying to plot. This explains why you do not see anything with your axis limits axis([0 3000 -2000 80000]); that by far exceed the range of y-values and obscures the non-zero data entries for small x.

Related

Generate a Rectangular Pulse in 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.

Using trapz to find the area under a curve

I am attempting to use trapz to find the area under the curve.
My x data represents datetime, and my y data represents acceleration as a f(x). Accelerometer readings are in the form of SI.
Example data within x ( HH:mm:ss.SSS ) :
'01:06:48.330'
'01:06:48.352'
'01:06:48.374'
Example data within y ( accelerometer value * 9.81 ):
8.73750256159470
8.59619296907904
8.63520263017352
When I enter the following command (using the whole array of data ):
velocity = trapz(x,y);
I get back a duration array which looks like this:
velocity =
duration
00:00:13
I am not entirely too sure I understand what 00:00:13 means. When I calculate for velocity, I'd expect to see something like 9.81 m/s , or 5m/s. Am I misusing the function or should I convert the duration array to a different object type?
The reason you expect m/s output from integrating acceleration is simply because you're doing a particular calculation involving (m/s^2)*s, i.e. y axis * x axis.
Let's use a simple example, where we first convert to seconds and then integrate.
x = datetime( {'00:00', '00:01', '00:02'}, 'inputformat', 'HH:mm' ); % Time
y = [1 2 4]; % Acceleration (m/s^2)
x_elapsed_seconds = datenum(x-min(x))*24*60*60; % Datenum returns days, convert to secs
z = trapz( x_elapsed_seconds, y ); % Velocity = 270 m/s
We can verify that, for this example, 270m/s is correct because there are simply 2 trapeziums in the calculation:
Trapezium from 1m/s^2 to 2m/s^2 lasting 1min = 60secs: 60*(1+2)/2 = 60*1.5 = 90 m/s
Trapezium from 2m/s^2 to 4m/s^2 lasting 1min = 60secs: 60*(2+4)/2 = 60*3 = 180 m/s
We sum the trapezium areas for the result: 90 + 180 = 270 as expected. This is why it's always best to use a simple example for verification before using real world data.
Datetimes are usually tricky in MATLAB. I would convert it to seconds before doing any integrals. Here is an example solution.
x=['01:06:48.330'; '01:06:48.352'; '01:06:48.374'];
datetime(x,'Format','HH:mm:ss.SSS');
secvec = datevec(x);
secvec = 3600*secvec(:,4)+60*secvec(:,5)+secvec(:,6); % times measured in seconds
y = [8.73750256159470; 8.59619296907904; 8.63520263017352]; % accelerations in m/s^2
trapz(secvec,y)
>> ans = 0.380216002428058 % Gained velocity, measured in m/s

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))

Resampling of time signal in MATLAB

I want to resample my signal with to new time. Currently the sampling time of my signal is 0.01s, and the size of my signal and time array is 1*90001.
I am trying to use resample(x,p,q) in MATLAB, but I am a little bit confused.
Can somebody suggest the right way to use this function and how to resample my data to rate of 0.02s instead of 0.01s?
Code - this is how I am trying to use resample, with example data.
t = [0:0.03:1];
x = sin(4*pi*t);
y = resample(x, 1, 2);
ty = resample(t,1,2);
figure (1);
stem(ty, y, 'r*');
hold on;
stem(t,x,'b')
hold off
Updated Code :
t = [0 2 3 7 8 9 10 11 12 17 18 19 20 24 25 26 27 28 29 31 32 33 35 37 41 ];
A = [0 0 1 2 3 5.2 0 -1.4 0 2 2.7 2 2.3 6 7.3 0 0 -8.6 0 1 1 2.5 3 4.8 2];
plot(t,A)
% Tx = min(diff(t));
Tx = 1:0.1:25;
B = interp1(t,A,Tx); %re-make example data to have decimal points on the x-axis
y = resample(B, 2, 1);
T = 0.05;
Ty = T / (2 / 1);
ty = (0:length(y)-1)*Ty;
% A = interp1(t,ref,t2);
% A = ref;
figure
plot(Tx,B,'b')
hold on
plot(ty,y,'r')
plot(t,A,'g')
hold off
First of all you do not need to resample time line.
It is much easier to define time sampling interval variable or sampling frequency variable: T = 0.03; Fs = 1/T;
So, x resampling you perform right way: y = resample(x, 1, 2); .
But the new time line must be reconstructed via adjusted sampling interval: Ty = T / (1 / 2); ty = (0:length(y)-1)*Ty;
The resample function is suitable only for uniformly time distributed data points. If your original points are non-uniformly distributed, you need:
Interpolate your x signal to the uniform time line with the smallest sampling interval from the original time line: Tx = min(diff(t)); . See for example interp1 function.
Resample your interpolated uniformly time distributed (sampled) signal to the new sampling interval ( resample function).
your original signal is sampled with uniform sampling interval of 10 ms and you want to decrease sampling down to 20 ms. Why don't you just take every second datapoint of your original signal?
y = x(1:2:end);
ty = t(1:2:end);
UPDATE
for non regularly spaced datasets it is possible to use function resample as it is shown here: https://au.mathworks.com/help/signal/ref/resample.html#bungoxs
you can try
fs = 1/0.02;
[y, ty] = resample(x, t, fs, 1, 2)
There is another way to resample in a lower frequency your data.
Use this code:
fs=1/(timesignal(2)-timesignal(1)); %for example 48000Hz
fs_resampled=100; % [Hz] example goal value
t_original = [0:1/fs:(1/fs*(length(signal)-1))];%current time signal
t_resampled = [0:1/fs_resampled:max(t_original)];%new time signal
Signal_resampled = interp1(t_original,signal,t_resampled,'spline');
I hope that's what you wanted.
Greetings

How can I create n sine waves from the elements of an n-by-m matrix?

I'm writing a program on MATLAB that generates 13 waveforms of varying amplitude, duration, and frequency. Each waveform is repeated 5 times, which means I have 65 'trials' in total.
The total length of each trial = 1.5 ms. The sampling frequency = 4 kHz. I would like the wave to begin at 0.5 ms. Prior to the onset of the wave, and following its offset, I would like the amplitude to be zero (i.e. a 'flatline' prior to and following the wave).
I have created a 65x3 matrix where the columns denote the frequency ('hz'), amplitude ('a'), and duration (ms) of the 65 sine waves. Each row denotes a single wave.
I would like to use the information contained in this 65x3 matrix to generate 65 sine waves of amplitude 'a', frequency 'hz', and duration 'ms'. To be specific: each wave should be created using the parameters (hz,a,ms) specified in the nth row of the matrix. E.g. if row 1 = 100, 1, 50... this means I would like to generate a 100 Hz sine wave (amplitude = 1) lasting 50 ms.
I have attempted to construct a for loop to solve this problem. However, the loop returns a number of errors, and I'm not sure how to resolve them. I have adapted the code to the point where no errors are returned; however, my latest attempt seems to generate 65 waves of equal duration, when in fact the duration of each wave should be that which is stated in vector 'ms'.
Here is my latest, albeit newbie and still unsuccessful, attempt: (note that 'trials' represents the 65x3 matrix discussed above; mA = amplitude).
hz=trials(:,1); mA=trials(:,2); ms=trials(:,3);
trials_waves=zeros(65,500); % the max duration (= 500ms); unsure of this part?
for n = 1:size(order,1)
trials_waves = mA*sin(2*pi*hz*0:ms);
end
Apologies if the information provided is scarce. This is the first time I have asked a question on this website. I can provide more information if needed.
Thank you for your help.
Best,
H
Looks like you've got a good start, I'll try to help you get further towards your solution.
Make a Sine Wave
For starters, let's make a sine wave with variable rate, amplitude, and length.
Fs = 4e3; % sample rate of 4 kHz
Sr = 100; % example rate
Sa = 1; % amplitude
St = 10e-3; % signal duration is 10 ms
% To create a sine wave in MATLAB, I'm going to first create a vector of time,
% `t`, and then create the vector of sine wave samples.
N = St * Fs; % number of samples = duration times sample rate;
t = (1:N) * 1/Fs; % time increment is one over sample rate
% Now I can build my sine wave:
Wave = Sa * sin( 2 * pi * Sr * t );
figure; plot(t, Wave);
Note! This is barely enough time for a full wavelength, so be careful with slow rates and short time lengths.
Make many Sine Waves
To turn this into a loop, I need to index into vectors of input variables. Using my previous example:
Fs = 4e3; % sample rate of 4 kHz
Sr = [100 200 300]; % rates
Sa = [1 .8 .5]; % amplitudes
St = [10e-3 20e-3 25e-3]; % signal durations
nWaves = length(Sr);
N = max(St) * Fs; % number of samples = duration times sample rate;
t = (1:N) /Fs; % time increment is one over sample rate
% initialize the array
waves = zeros(nWaves, N);
for iWaves = 1:nWaves
% index into each variable
thisT = (1:St(iWaves) * Fs) * 1/Fs;
myWave = Sa(iWaves) * sin( 2 * pi * Sr(iWaves) * thisT );
waves(iWaves,1:length(myWave)) = myWave;
end
figure; plot(t, waves);
You still have one more piece, zero padding the front end of your signals, there's lots of ways to do it, one way would be to build the signal the way I've described and then concatenate an appropriate number of zeros to the front of your signal array. Feel free to ask a new question if you get stuck. Good luck!