Changing the frequency of an array does not work always

Hi All
I have a time array , from which I first drop some elements, then change its frequency based on the input I give. the array is X in the code. what happens is that for some specific values , like reduction = 2 and freq = 1000 Hz if the original frequency of X was 512 Hz, the length of redt2 falls one element shorter than the original X array. how does this happen and what is the solution.
X is any time signal, 1D array with any length and frequency
reduction =2
X =X (1:reduction:numel(X));
redt1=[X];
rf= redt1(2)-redt1(1);
tend=redt1(numel(redt1));
freq= input(frequency)
dt= 1/freq
redt2= 0:dt:tend/rf/freq;

8 Comments

Is this MATLAB coding related question or subjective issue?
I pasted the code here. if it happens for you too, it can be generic, but It is about lacking one element wihen dropping one other element , it does not happen if you drop every 3 or n elements, only in every 2 element.
please try putting reduction 2 ,3 ,4 ,. .. and you will see that only reduction 2 has problem. strange for me
X is any time signal, 1D array with any length and frequency
So, could you please help me out?
Does it happen for you

Sign in to comment.

 Accepted Answer

In
freq= input(frequency)
what is frequency? Some string you defined to ask the user for the frequency? Please show how you defined it.
reduction = 2
X_Original = 1 : 1000;
whos X
samplingFreqOriginal = (X_Original(end) - X_Original(1)) / numel(X_Original)
X_Reduced = X_Original(1:reduction:numel(X_Original));
whos X
samplingFreqReduced = (X_Reduced(end) - X_Reduced(1)) / numel(X_Reduced) % Will be 2 times samplingFreqOriginal
% Not sure what anything after here is intended to do.
redt1 = X_Reduced;
rf = redt1(2) - redt1(1)
tend = redt1(numel(redt1))
frequency = 'Enter the frequency : ';
freq = input(frequency)
dt = 1 / freq
redt2 = 0:dt:tend/rf/freq;

9 Comments

Thank you very much.
in
freq= input('frequency')
the freq can be anything. using my code, I was trying to first : drop some elements from a time array, then define its new frequency myself. since if you have a time array and you drop every second element, and your time array is from 0 to 30, after dropping those elements, your time array will still be 30 seconds, I wanted to decide the frequency of the time array myself, since I am doing the same dropping on the Y axis, so the X and Y should have the same size to replot again.
rf = redt1(2) - redt1(1)
1/rf will be the original frequency of the time array and
tend/rf/freq
will tell the last element of the new time array based on the desired frequency : freq
this works for all reduction rates, except for 2
by the way, as per your code I dont want to use the
samplingFreqReduced
and want to change it and apply it to the time array reduced
Try this:
frequencyPrompt = 'Enter the frequency reduction factor: ';
reduction = input(frequencyPrompt)
X_Original = 1 : 1000;
whos X
samplingFreqOriginal = (X_Original(end) - X_Original(1)) / numel(X_Original)
% Reduce the sampling frequency.
newNumberOfElements = round(numel(X_Original) / reduction)
X_Reduced = linspace(min(X_Original), max(X_Original), newNumberOfElements);
whos X
samplingFreqReduced = (X_Reduced(end) - X_Reduced(1)) / numel(X_Reduced)
Thank you, not there yet, doing so , I want to be able to change the frequency of X_Reduced, like in your example, the last member is still 1000 ( while its length is reduced to 500) , while if I double its sampling frequency, it should get 500 as last member with also 500 length
If you just crop the array by half, the last element will be 500 and the sampling rate will be the same (1). If you want to both reduce the sampling rate and reduce the overall time or distance (Which does your X represent) from 1000 elements to 500 elements, then you'll have to compute both the new stopping X, and the new number of elements and change them in linspace().
Shall you please tell me how you would do that? Cause the previous method was working for all but not for reduction=2.
You can't specify
  1. the initial and final values
  2. the sampling interval (delta x between adjacent elements)
  3. the number of elements
all independently. Once you specify 2, the third is determined. Which two do you want to specify?
So , for my plot, for the vertical axis, I drop some elements, but I can not touch anything. for the time axis, I want it to start from 0 , but the end can be determined based on the frequency.
so I want to determine number 2 and 3 , but starting from 0
Then simply do
% Sample values:
startingValue = 1
deltaX = 3
numElements = 9
% Create array:
x = startingValue : deltaX : (startingValue + (numElements - 1) * deltaX)
fprintf('x is %d elements long.\n', length(x));

Sign in to comment.

More Answers (0)

Asked:

on 27 Nov 2020

Commented:

on 28 Nov 2020

Community Treasure Hunt

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

Start Hunting!