C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6 MSBUILD

# increase / decrease the frequency of a signal using fft and ifft in matlab / octave

Date : November 22 2020, 10:33 AM
I think the issue was by ths following , I apologize that the following is a bit messy. I did everything manually because I'm not sure how else to do it.
First you need to know how MATLAB stores frequency domain data. Take the following example:
code :
``````N = 100;            % number of samples
Fs = 100;           % sampling frequency
f = 5;              % frequency of the signal
t = 0:1/N:1-1/N;    % time goes from 0 to 1 second
y = cos(2*pi*f*t);  % time signal
Y = fft(y);         % frequency signal

figure(1); plot(y);
figure(2); plot(abs(Y));
``````
``````clear all,clf

Fs = 100;% Sampling frequency
t=linspace(0,1,Fs);

%1a create signal
ya = .5*sin(2*pi*1*t);

%2a create frequency domain
ya_fft = fft(ya);

mag = abs(ya_fft);
phase = unwrap(angle(ya_fft));
ya_newifft=ifft(mag.*exp(i*phase));

% ----- changes start here ----- %

shift   = 1;                            % shift amount
N       = length(ya_fft);               % number of points in the fft
mag1    = mag(2:N/2+1);                 % get positive freq. magnitude
phase1  = phase(2:N/2+1);               % get positive freq. phases
mag2    = mag(N/2+2:end);               % get negative freq. magnitude
phase2  = phase(N/2+2:end);             % get negative freq. phases

% pad the positive frequency signals with 'shift' zeros on the left
% remove 'shift' components on the right
mag1s   = [zeros(1,shift) , mag1(1:end-shift)];
phase1s = [zeros(1,shift) , phase1(1:end-shift)];

% pad the negative frequency signals with 'shift' zeros on the right
% remove 'shift' components on the left
mag2s   = [mag2(shift+1:end), zeros(1,shift)];
phase2s = [phase2(shift+1:end), zeros(1,shift) ];

% recreate the frequency spectrum after the shift
%           DC      +ve freq.   -ve freq.
magS    = [mag(1)   , mag1s     , mag2s];
phaseS  = [phase(1) , phase1s   , phase2s];

x = magS.*cos(phaseS);                  % change from polar to rectangular
y = magS.*sin(phaseS);
ya_fft2 = x + i*y;                      % store signal as complex numbers
ya_ifft2 = real(ifft(ya_fft2));         % take inverse fft

plot(t,ya_ifft2);                       % time signal with increased frequency
``````

Share :

## How can I increase/decrease (frequency/pitch) of signal

By : ting.c
Date : March 29 2020, 07:55 AM
it should still fix some issue This is one way to do this but if the signal is large you may have to increase the number of points to the signal
code :
``````clear,clc
fs = 44100;                   % Sampling frequency
t=linspace(0,1,fs);
freq=1;
ya = sin(2*pi*freq*t)'; %+ 1*sin(2*pi*250*t);

num_per_sec=5
yb=repmat(ya,num_per_sec,1);%replicate matrix
xxo=linspace(0,1,length(yb))'; %go from 0 to 1 sec can change speed by incr/decr 1
xxi=linspace(0,1,length(ya))'; %go from 0 to 1 sec and get total samplerate from total y value
yi_t=interp1(xxo,yb,xxi,'linear');

plot(yi_t)
``````

## fft/ifft: Sampling Frequency and Length of Signal

By : Zamela
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further When you use the abs(fft()) in ifft, you are using only the amplitude of the signal and dropping the phase information, which is needed.
Use the whole signal (removed abs):
code :
``````plot(real(ifft(fft(x)))); % fft then ifft
``````

## MATLAB: FFT a signal to frequency and IFFT back to time domain, not exactly the first signal

By : Peter Donnellan
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further That's because you are modifying the length of the FFT compared with the length of your time axis. To see this, replace
code :
``````nfft=2^nextpow2(length(Efield));
``````
``````nfft=length(Efield);
``````

## Using interpolation and repmat to change frequency of signal in octave and matlab

By : michaeltonight
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further You are trying to call repmat with a floating point number. Obviously it won't work the way you intended. repmat's intended operation is to replicate a particular dimension an integer number of times.
Therefore, one thing I can suggest is to perhaps truncate the signal for the multiple which doesn't go up to 1 and stack this at the end of the replicated signal. For example, if you wanted to replicate a signal 2.4 times, you'd replicate the entire signal twice normally, then stack 40% of the length of the signal to the end of the array. Therefore, you'd sample up to 40% of the total duration of the signal and place this at the end of the replicated signal.
code :
``````%// Your code
clear, clc
fs = 44100; %// Define sampling frequency
t=linspace(0,2*pi,fs);
freq=1;
ya = sin(freq*t)'; %// Define signal

num_per_sec=2.1; %// Define total number of times we see the signal

%// New code
%// Get total number of integer times we see the signal
num_whole = floor(num_per_sec);

%// Replicate signal
yb=repmat(ya,num_whole,1);

%// Determine how many samples the partial signal consists of
portion = floor((num_per_sec - num_whole)*fs);

%// Sample from the original signal and stack this on top of replicated signal
yb = [yb; ya(1:portion)];