科学音频处理（二）：如何使用 Octave 对音频文件进行基本数学信号处理
音频 Octave
在[前一篇的指导教程](/article-7755-1.html)中，我们看到了读、写以及重放音频文件的简单步骤，我们甚至看到如何从一个周期函数比如余弦函数合成一个音频文件。在这篇指导教程中，我们将会看到如何对信号进行叠加和倍乘（调整），并应用一些基本的数学函数看看它们对原始信号的影响。


![](/data/attachment/album/201609/30/142614fv37zcv20r20svvs.jpg)


### 信号叠加


两个信号 S1（t）和 S2（t）相加形成一个新的信号 R(t)，这个信号在任何瞬间的值等于构成它的两个信号在那个时刻的值之和。就像下面这样：



```
R(t) = S1(t) + S2(t)

```

我们将用 Octave 重新产生两个信号的和并通过图表看达到的效果。首先，我们生成两个不同频率的信号，看一看它们的叠加信号是什么样的。


#### 第一步：产生两个不同频率的信号（oog 文件）



```
>> sig1='cos440.ogg';                  %creating the audio file @440 Hz
>> sig2='cos880.ogg';                  %creating the audio file @880 Hz
>> fs=44100;                           %generating the parameters values (Period, sampling frequency and angular frequency)
>> t=0:1/fs:0.02;
>> w1=2*pi*440*t;
>> w2=2*pi*880*t;
>> audiowrite(sig1,cos(w1),fs);        %writing the function cos(w) on the files created
>> audiowrite(sig2,cos(w2),fs);

```

然后我们绘制出两个信号的图像。


**信号 1 的图像（440 赫兹）**



```
>> [y1, fs] = audioread(sig1);
>> plot(y1)

```

[![信号 1 的图像](/data/attachment/album/201609/30/142623bjmr212im84nnjkr.png)](https://www.howtoforge.com/images/octave-audio-signal-processing-ubuntu/big/plotsignal1.png)


**信号 2 的图像（880 赫兹）**



```
>> [y2, fs] = audioread(sig2);
>> plot(y2)

```

[![信号 2 的图像](/data/attachment/album/201609/30/142624p9pmpphiddyj9jr5.png)](https://www.howtoforge.com/images/octave-audio-signal-processing-ubuntu/big/plotsignal2.png)


#### 第二步：把两个信号叠加


现在我们展示一下前面步骤中产生的两个信号的和。



```
>> sumres=y1+y2;
>> plot(sumres)

```

叠加信号的图像


[![和信号的图像](/data/attachment/album/201609/30/142626hy5c6yyieigzmi59.png)](https://www.howtoforge.com/images/octave-audio-signal-processing-ubuntu/big/plotsum.png)


**Octaver 中的效果**


在 Octaver 中，这个效果产生的声音是独特的，因为它可以仿真音乐家弹奏的低八度或者高八度音符（取决于内部程序设计），仿真音符和原始音符成对，也就是两个音符发出相同的声音。


#### 第三步：把两个真实的信号相加（比如两首音乐歌曲）


为了实现这个目的，我们使用<ruby> 格列高利圣咏 <rp>  （ </rp> <rt>  Gregorian Chants </rt> <rp>  ） </rp></ruby>中的两首歌曲（声音采样）。


<ruby> 圣母颂曲 <rp>  （ </rp> <rt>  Avemaria Track </rt> <rp>  ） </rp></ruby>


首先，我们看一下圣母颂曲并绘出它的图像：



```
>> [y1,fs]=audioread('avemaria_.ogg');
>> plot(y1)

```

[![圣母歌曲](/data/attachment/album/201609/30/142627jdkizdd4jhc68fzd.png)](https://www.howtoforge.com/images/octave-audio-signal-processing-ubuntu/big/avemaria.png)


<ruby> 赞美诗曲 <rp>  （ </rp> <rt>  Hymnus Track </rt> <rp>  ） </rp></ruby>


现在我们看一下赞美诗曲并绘出它的图像。



```
>> [y2,fs]=audioread('hymnus.ogg');
>> plot(y2)

```

[![赞美诗曲](/data/attachment/album/201609/30/142628vif2uzfdfcdcy2gf.png)](https://www.howtoforge.com/images/octave-audio-signal-processing-ubuntu/big/hymnus.png)


**圣母颂曲 + 赞美诗曲**



```
>> y='avehymnus.ogg';
>> audiowrite(y, y1+y2, fs);
>> [y, fs]=audioread('avehymnus.ogg');
>> plot(y)

```

[![圣母歌曲 + 赞美诗曲](/data/attachment/album/201609/30/142629fbz22rizro22cijo.png)](https://www.howtoforge.com/images/octave-audio-signal-processing-ubuntu/big/avehymnus.png)


结果，从音频的角度来看，两个声音信号混合在了一起。


### 两个信号的乘积


对于求两个信号的乘积，我们可以使用类似求和的方法。我们使用之前生成的相同文件。



```
R(t) = S1(t) * S2(t)

```


```
>> sig1='cos440.ogg';                  %creating the audio file @440 Hz
>> sig2='cos880.ogg';                  %creating the audio file @880 Hz
>> product='prod.ogg';                 %creating the audio file for product
>> fs=44100;                           %generating the parameters values (Period, sampling frequency and angular frequency)
>> t=0:1/fs:0.02;
>> w1=2*pi*440*t;
>> w2=2*pi*880*t;
>> audiowrite(sig1, cos(w1), fs);      %writing the function cos(w) on the files created
>> audiowrite(sig2, cos(w2), fs);>> [y1,fs]=audioread(sig1);>> [y2,fs]=audioread(sig2);
>> audiowrite(product, y1.*y2, fs);    %performing the product
>> [yprod,fs]=audioread(product);
>> plot(yprod);                        %plotting the product

```

注意：我们必须使用操作符 ‘`.*`’，因为在参数文件中，这个乘积是值与值相乘。更多信息，请参考 Octave 矩阵操作产品手册。


#### 乘积生成信号的图像


[![乘积信号的图像](/data/attachment/album/201609/30/142629e0twfm4gvn8zvjvb.png)](https://www.howtoforge.com/images/octave-audio-signal-processing-ubuntu/big/plotprod.png)


#### 两个基本频率相差很大的信号相乘后的图表效果（调制原理）


**第一步：**


生成两个频率为 220 赫兹的声音信号。



```
>> fs=44100;
>> t=0:1/fs:0.03;
>> w=2*pi*220*t;
>> y1=cos(w);
>> plot(y1);

```

[![载波](/data/attachment/album/201609/30/142630lb4ggglzw1mh01wb.png)](https://www.howtoforge.com/images/octave-audio-signal-processing-ubuntu/big/carrier.png)


**第二步：**


生成一个 22000 赫兹的高频调制信号。



```
>> y2=cos(100*w);
>> plot(y2);

```

[![调制中](/data/attachment/album/201609/30/142630ojjjb2wcs283vvjj.png)](https://www.howtoforge.com/images/octave-audio-signal-processing-ubuntu/big/modulating.png)


**第三步：**


把两个信号相乘并绘出图像。



```
>> plot(y1.*y2);

```

[![调制后的信号](/data/attachment/album/201609/30/142631jkmequ4qq748lruk.png)](https://www.howtoforge.com/images/octave-audio-signal-processing-ubuntu/big/modulated.png)


### 一个信号和一个标量相乘


一个函数和一个标量相乘的效果等于更改它的值域，在某些情况下，更改的是相标志。给定一个标量 K ，一个函数 F(t) 和这个标量相乘定义为：



```
R(t) = K*F(t)

```


```
>> [y,fs]=audioread('cos440.ogg');        %creating the work files
>> res1='coslow.ogg';                
>> res2='coshigh.ogg';>> res3='cosinverted.ogg';
>> K1=0.2;                                %values of the scalars
>> K2=0.5;>> K3=-1;
>> audiowrite(res1, K1*y, fs);            %product function-scalar
>> audiowrite(res2, K2*y, fs);
>> audiowrite(res3, K3*y, fs);

```

**原始信号的图像**



```
>> plot(y)

```

[![](/data/attachment/album/201609/30/142631e0z3d41a4mmt1tzz.png)](https://www.howtoforge.com/images/octave-audio-signal-processing-ubuntu/big/originalsignal.png)


**信号振幅减为原始信号振幅的 0.2 倍后的图像**



```
>> plot(res1)

```

[![低余弦](/data/attachment/album/201609/30/142631ld3q7ht3l8ddli71.png)](https://www.howtoforge.com/images/octave-audio-signal-processing-ubuntu/big/coslow.png)


**信号振幅减为原始振幅的 0.5 倍后的图像**



```
>> plot(res2)

```

[![高余弦](/data/attachment/album/201609/30/142632bbh49di239m4pd4x.png)](https://www.howtoforge.com/images/octave-audio-signal-processing-ubuntu/big/coshigh.png)


**倒相后的信号图像**



```
>> plot(res3)

```

[![倒相余弦](/data/attachment/album/201609/30/142632ahi48hzvzw8wzpjj.png)](https://www.howtoforge.com/images/octave-audio-signal-processing-ubuntu/big/cosinverted.png)


### 结论


基本数学运算比如代数和、乘，以及函数与常量相乘是更多高级运算比如谱分析、振幅调制，角调制等的支柱和基础。在下一个教程中，我们来看一看如何进行这样的运算以及它们对声音文件产生的效果。




---


via: <https://www.howtoforge.com/tutorial/octave-audio-signal-processing-ubuntu/>


作者：[David Duarte](https://www.howtoforge.com/tutorial/octave-audio-signal-processing-ubuntu/) 译者：[ucasFL](https://github.com/ucasFL) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
# Scientific Audio Processing, Part II - How to make basic Mathematical Signal Processing in Audio files using Ubuntu with Octave 4.0

In the [previous tutorial](https://www.howtoforge.com/tutorial/how-to-read-and-write-audio-files-with-octave-4-in-ubuntu/), we saw the simple steps to read, write and playback audio files. We even saw how we can synthesize an audio file from a periodic function such as the cosine function. In this tutorial, we will see how we can do additions to signals, multiplying signals (modulation), and applying some basic mathematical functions to see their effect on the original signal.

## Adding Signals

The sum of two signals S1(t) and S2(t) results in a signal R(t) whose value at any instant of time is the sum of the added signal values at that moment. Just like this:

R(t) = S1(t) + S2(t)

We will recreate the sum of two signals in Octave and see the effect graphically. First, we will generate two signals of different frequencies to see the signal resulting from the sum.

### Step 1: Creating two signals of different frequencies (ogg files)

>> sig1='cos440.ogg'; %creating the audio file @440 Hz

>> sig2='cos880.ogg'; %creating the audio file @880 Hz

>> fs=44100; %generating the parameters values (Period, sampling frequency and angular frequency)

>> t=0:1/fs:0.02;

>> w1=2*pi*440*t;

>> w2=2*pi*880*t;

>> audiowrite(sig1,cos(w1),fs); %writing the function cos(w) on the files created

>> audiowrite(sig2,cos(w2),fs);


Here we will plot both signals.

**Plot of Signal 1 (440 Hz)**

>> [y1, fs] = audioread(sig1);

>> plot(y1)

**Plot of Signal 2 (880 Hz)**

>> [y2, fs] = audioread(sig2);

>> plot(y2)

### Step 2: Adding two signals

Now we perform the sum of the two signals created in the previous step.

>> sumres=y1+y2;

>> plot(sumres)

Plot of Resulting Signal

**The Octaver Effect**

In the Octaver, the sound provided by this effect is characteristic because it emulates the note being played by the musician, either in a lower or higher octave (according as it has been programmed), coupled with sound the original note, ie two notes appear identically sounding.

### Step 3: Adding two real signals (example with two musical tracks)

For this purpose, we will use two tracks of Gregorian Chants (voice sampling).

**Avemaria Track**

First, will read and plot an Avemaria track:

>> [y1,fs]=audioread('avemaria_.ogg');

>> plot(y1)

**Hymnus Track**

Now, will read and plot an hymnus track

>> [y2,fs]=audioread('hymnus.ogg');

>> plot(y2)

**Avemaria + Hymnus Track**

>> y='avehymnus.ogg';

>> audiowrite(y, y1+y2, fs);

>> [y, fs]=audioread('avehymnus.ogg');

>> plot(y)


The result, from the point of view of audio, is that both tracks will sound mixed.

## Product of two Signals


To multiply two signals, we have to use an analogous way to the sum. Let´s use the same files created previously.

>> sig1='cos440.ogg'; %creating the audio file @440 Hz

>> sig2='cos880.ogg'; %creating the audio file @880 Hz

>> product='prod.ogg'; %creating the audio file for product

>> fs=44100; %generating the parameters values (Period, sampling frequency and angular frequency)

>> t=0:1/fs:0.02;

>> w1=2*pi*440*t;

>> w2=2*pi*880*t;

>> audiowrite(sig1, cos(w1), fs); %writing the function cos(w) on the files created

>> audiowrite(sig2, cos(w2), fs);

>> [y1,fs]=audioread(sig1);

>> [y2,fs]=audioread(sig2);

>> audiowrite(product, y1.*y2, fs); %performing the product

>> [yprod,fs]=audioread(product);

>> plot(yprod); %plotting the product


Note: we have to use the operand '.*' because this product is made, value to value, on the argument files. For more information, please refer to the manual of product operations with matrices of Octave.

### Plot of Resulting Product Signal

### Graphical effect of multiplying two signals with a big fundamental frequency difference (Principles of Modulation)

**Step 1:**

Create an audio frequency signal with a 220Hz frequency.

>> fs=44100;

>> t=0:1/fs:0.03;

>> w=2*pi*220*t;

>> y1=cos(w);

>> plot(y1);

**Step 2:**

Create a higher frequency modulating signal of 22000 Hz.

>> y2=cos(100*w);

>> plot(y2);

**Step 3:**

Multiplying and plotting the two signals.

>> plot(y1.*y2);

## Multiplying a signal by a scalar

The effect of multiplying a function by a scalar is equivalent to modify their scope and, in some cases, the sign of the phase. Given a scalar K, the product of a function F(t) by the scalar is defined as:

>> [y,fs]=audioread('cos440.ogg'); %creating the work files

>> res1='coslow.ogg';

>> res2='coshigh.ogg';

>> res3='cosinverted.ogg';

>> K1=0.2; %values of the scalars

>> K2=0.5;

>> K3=-1;

>> audiowrite(res1, K1*y, fs); %product function-scalar

>> audiowrite(res2, K2*y, fs);

>> audiowrite(res3, K3*y, fs);

**Plot of the Original Signal**

>> plot(y)

![](data:image/svg+xml,%3Csvg%20xmlns=%22http://www.w3.org/2000/svg%22%20width=%22550%22%20height=%22291%22%3E%3C/svg%3E)


**Plot of a Signal reduced in amplitude by 0.2**

>> plot(res1)

**Plot of a Signal reduced in amplitude by 0.5**

>> plot(res2)

**Plot of a Signal with inverted phase**

>> plot(res3)

## Conclusion


The basic mathematical operations, such as algebraic sum, product, and product of a function by a scalar are the backbone of more advanced operations among which are, spectrum analysis, modulation in amplitude, angular modulation, etc. In the next tutorial, we will see how to make such operations and their effects on audio signals.