MATLAB Answers

Plotting two arrays of different lengths

47 views (last 30 days)
Sam Thorpe
Sam Thorpe on 30 Mar 2019
Answered: Agnish Dutta on 9 Apr 2019
Hi. I am currently working on a signal processing task where I have to plot the transmission loss of a steel pipe. So far I have all my data processed and am at the plotting stage. I was given a set of data including time and provided a function to convert this to a frequency domain using a fast fourier transform. When I come to plot my velocities against the frequency, I find that the frequency array is now half its original size after using the function. Is it possible to stretch the frequency array out so it again matches the size of the original input data. The original vector had 16384 elements in it but now has 8192. my current code is :
a=Acceleration; %assigning single values to each vector for ease of programming
%the following code is used to convert the acceleration into velocity using
%the cumsum function. The first step involves finding the value of the time
b = numel(t); %to determine the number elements in the array
c = t(end); %to find the final time value in the array (final time reading)
dt = c/b; %divides the largest time value by the number of elements to get
%the time step
vt = cumsum(a)*dt; %this equation uses the cumsum function to turn the
%acceleration into velocity so it can be compared to the measured velocity
av = myfft(t,v); %the following script calls in the function to perform
%the fast fourier transform of the velocity and the converted acceleration
bv = myfft(t,vt); %In both cases, the outcome of the transform is the same
%this is to be expected as the time is still the same for both velocities
tlv = 20*log(v); %equation to convert the velocity into decibles for processing
tlvt = 20*log(vt);
transmissionloss = tlv-tlvt %vector of transmission loss
hold on
figure (1)
hold off
the function myfft is as followed
%Use this function to transform time domain data into the frequency domain.
%Input variables:
%Time - time interval
%Data - vector of time domain data
%Return variables
%freq - frequency interval
%fData- data transformed into the frequency domain
function [freq, fData] = myfft(Time,Data)
N = length(Data); %length of the data array
fs = 1/(Time(2)-Time(1)); %sampling frequency
freq = (1:N/2+1)/N*fs; %frequency interval
fData = fft(Data); %transformation of time domain into frequency domain
fData = fData(1:N/2+1); %prepare data for return

Answers (1)

Agnish Dutta
Agnish Dutta on 9 Apr 2019
From what I understand, you want to increase the size of a vector to match a specified value, without losing any information. I believe the best way, this can be done is through interpolation.
Here's some code that shows how a vector can be expanded to an arbitrary size, by calculating the intermediate values via interpolation.
% new_size is the length you want the interpolated vector to be.
% x is the vector you want to "strech".
x = 0 : pi/6 : pi;
L = length(x);
new_size = 50;
X = interp1(1:L, x, linspace(1, L, new_size));
You can set the type of interpolation you want by setting the "method" parameter of the "interp1" function as shown in the following document:

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!